Back to Course

파이썬 소개

0% Complete
0/0 Steps
  1. 코딩의 기초
    소단원 1: 파이썬 소개
    4 Topics
    |
    1 Quiz
  2. 수업 2: 파이썬을 사용한 애니메이션
    4 Topics
    |
    1 Quiz
  3. 수업 3: 알고리즘 및 순서도
    5 Topics
    |
    1 Quiz
  4. 파이썬을 사용한 프로그래밍 개념
    수업 4: 변수 및 산술 연산자
    6 Topics
    |
    1 Quiz
  5. 수업 5: 파이썬의 함수
    6 Topics
    |
    1 Quiz
  6. 소단원 6: 조건부 프로그래밍
    5 Topics
    |
    1 Quiz
  7. 수업 7: 파이썬의 루프 - While 루프
    3 Topics
    |
    1 Quiz
  8. 수업 8: 파이썬의 루프 - For 루프
    3 Topics
    |
    1 Quiz
  9. 수업 9: 문자열 작업
    5 Topics
    |
    1 Quiz
  10. 수업 10: 파이썬의 목록
    4 Topics
    |
    1 Quiz
  11. 파이썬 게임
    수업 11: 미로 게임의 딱정벌레
    4 Topics
  12. 캡스톤 프로젝트
    수업 12: 캡스톤 프로젝트
Lesson 9, Topic 2
In Progress

문자열 함수

Lesson Progress
0% Complete

문자열 작업

OperatorDescriptionExample
+ (Concatenation)The + operator joins the text on both sides of the operator.print("Save " + "Earth")
>> Save Earth

To give a white space between the two words, insert a space before the closing single quote of the first literal.
* (Repetition)The * operator repeats the string on the left-hand side times the value on the right-hand side.print(3 * "Hello")
>> HelloHelloHello
in (Membership)The operator displays 1 if the string contains the given character or the sequence of characters.A = "Save Earth"
print('S' in A)
>> True

print('Save' in A)
>> True

print('SE' in A)
>> False
Slice[n:m]The Slice[n : m] operator extracts sub-parts from the strings.A = "Save Earth"
print(A[1:3])
>> av

The print statement prints the substring starting from subscript 1 and ending at subscript 3 but not including subscript 3.

print(A[3:])
>> e Earth

Omitting the second index directs the python interpreter to extract the substring till the end of the string.

print(A[:3])
>> Sav

Omitting the first index directs the python interpreter to extract the substring before the second index starting from the beginning.

print(A[:])
>> Save Earth

Omitting both the indices directs the python interpreter to extract the entire string starting from 0 till the last index.

문자열 메서드 및 내장 함수

SyntaxDescriptionExample
len()Returns the length of the string.A = "Save Earth"
print(len(A))
>> 10
capitalize()Returns the exact copy of the string with the first letter in upper case.A = "save earth"
print(A.capitalize())
>> Save Earth
lower()Returns the exact copy of the string with all the letters in lowercase.A = "Save Earth"
print(A.lower())
>> save earth
upper()Returns the exact copy of the string with all letters in uppercase.A = "save earth"
print(A.upper())
>> SAVE EARTH
find(sub[start[, end]])The function is used to search for the first occurrence of the substring in the given string. It returns the index at which the substring starts. It returns -1 if the substring does occur in the string.A = "Save Earth"
print(A.find('Sa'))
>> 0
On omitting the start parameters, the function starts the search from the beginning.

print(A.find('Sa', 2))
>> -1
Displays -1 because the substring could not be found between the string's index 2 and the end.
replace(old, new)The function replaces all the occurrences of the old string with the new string. A = "Save Earth"
print(A.replace("Save", "Mother"))
>> Mother Earth
join()Returns a string in which the string elements have been joined by a separator.str1 = ('Jan', 'Feb', 'Mar')
str2 = ' '
print(str2.join(str1))
>> Jan Feb Mar
split([sep[, maxsplit]])The function splits the string into substrings using the separator. The second argument is optional and its default value is zero. If an integer value N is given for the second argument, the string is split into N+1 strings.str='The$earth$is$what$we$all$have$in$common'
print(str.split('$',3))
>> ['The', 'earth', 'is', 'what$we$all$have$in$common.']

print(str.split('$'))
>>['The', 'earth', 'is', 'what', 'we', 'all', 'have', 'in', 'common.']

참고: 위의 표에서 len( )은 내장 함수이므로 문자열 모듈을 가져올 필요가 없습니다. 다른 모든 함수의 경우 성공적으로 실행하려면 import string 문이 필요합니다.