String manipulation example in Python3

String manipulation example in Python3

# -*- coding:utf-8 -*- 
str = 'abcdededededefg' #Get

data according to the index, 0 to see the first character, -1 to see the first character from the right
print(str[0])
print( str[-1]) #String

interception/slicing
print(str[2:]) # cdededededefg, intercept from the character with index 2 until the end
print(str[1:3]) # bc, from index 1 The character of the 3-1 position is intercepted, and the character at the position of 3 is not included.
print(str[:3]) # abc , the character at the position of 3-1 is intercepted from the beginning, and the character at the position of 3 is not included.
print(str[0:6: 2]) # ace , intercept the character with index 0 to 6-1 position, excluding the character at 6 position, the slice step size is 2, and intercept one every two characters
print(str[::-1]) # gfedededededcba, string flashback output #Use

index to get the index of the character
print(str.index('c'))

#Find S.find(sub[, start[, end]]) -> int can search for strings, according to start ----end Search in the slice range
print(str.find('f'))
print(str.find('de',6,9))

# strip can remove the spaces at the beginning and end of the string, the middle content is not will delete
f = ' jj ll '
print(f.strip()) #Get

the length of the string
Use the character 'b' as a splitter to generate a list #S.join(iterable)->str returns a string which is a concatenated iterable of strings. The separator between elements is s.



















print('b'.join(b)) #returns a string abcebcegbs

#S.capitalize()-> str returns an uppercase S, which is the first character, with uppercase letters and other lowercase letters.
print(str.capitalize()) #return: Abcdededededefg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937803&siteId=291194637