Lesson Progress
0% Complete
문자열 작업
Operator | Description | Example |
---|---|---|
+ (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. |
문자열 메서드 및 내장 함수
Syntax | Description | Example |
---|---|---|
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 문이 필요합니다.