4. String manipulation in python development

1. Set a string
name = 'my name is caibaofei'
 
name.capitalize() #The first letter is capitalized
name.count("a") #Count the number of a in the string
name.center(50,"-") #Print 50 characters in total, not enough to fill in with "-" on both sides
name.endswith("fei") # Determine the end of the string, return True if it is correct, and false if it is wrong
name.find('n') #find the subscript of n in the string, which can be used as a slice
name.format() #Convert the variable or %s, {0} in the string into the corresponding variable
name.format_map({'name':'cai','year':'23'} #Set a converted dictionary to the variables in the string
name.isalnum() #To determine whether it is an Arabic number or a string, it returns True, not false
name.isalpha() #To determine whether it is an English character, it returns True, not false
name.isdecimal() #To determine whether it is decimal, it returns True, not false
name.isdigit() #To determine whether it is an integer, it returns True, not false
name.isidentifier() #To determine whether it is a valid variable name, it returns True, not false
name.islower() #To determine whether it is lowercase, it returns True, not false
name.isspace() #To determine whether it is a space, it returns True, not false
name.istitle() #To determine whether each first letter is uppercase, it returns True, not false
name.ljust(50,'*') #Complete left, count 50 numbers from left to right, if it is not enough, use * to complete (Note: after completion * is on the right)
name.rjust(50,'*') #Complete right, count 50 numbers from right to left, if it is not enough, use * to complete (Note: after completion, * is on the left)
name.lower() # Convert uppercase in string to lowercase
name.upper() # Convert lowercase to uppercase in the string
name.lstrip() #Remove left spaces and carriage returns
name.rstrip() #Remove spaces and carriage returns on the right
name.strip() #Remove spaces and carriage returns on both sides
name.replace('c','f',1) #Replace c in the string with f, and only replace the first c
name.rfind('c') #Find the subscript of the rightmost c from left to right
name.split() #Split the string into a list by spaces
name.split('c') #Split the string into a list by c  
name.splitlines() #Split the string into a list by newline
name.swapcase() #The case will be swapped
name.title() # Convert all strings to uppercase
name.zfill(50) #Print 50 numbers of strings, use 0 to fill the insufficient positions
 
p = str.maketrans('abcdef','123456')
name.translate(p) #According to the corresponding relationship set by p above, replace the value in the string

Guess you like

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