Related operations on python strings



 

PYTHON string related operations

 

1. String search name = "today is a\t beautiful day" print(name.capitalize()) # Capitalize the first letter print(name.center(40,'-')) # Center display , fill in '-' when it is insufficient print(name.count('a',4)) #Count the number of occurrences of a character print(name.endswith('a')) #Judging whether it ends with a given character, yes Return True, otherwise return False print(name.startswith('t')) #To determine whether it starts with the given character, it returns True, otherwise it returns False print(name.expandtabs(30)) #Set the length of the tab print( name.find('is')) #Find the index of the first occurrence of the string print(name.rfind('a')) #The index of the last occurrence of a given character print(name.index('a')) #Give The index of the first occurrence of a given character print(name.rindex('a')) #The index of the last occurrence of a given character print(name.swapcase()) #Case conversion from small to large, large to small print(name .replace('s','S')) #String replacement print(name.title())#First letter uppercase print(name.lower()) #All lowercase print(name.upper()) #All uppercase 

2. String judgment name3 = '123afdasd' print(name3.isdigit()) #judging whether it is an integer print(name3.isalnum()) #is it letters and numbers, Arabic print(name3.isalpha()) #is it Letter print(name3.isdecimal()) # Is it a decimal number print(name3.isidentifier()) #Determine whether it is a legal identifier print(name3.isnumeric()) #Is it a number print(name3.islower()) #Whether it is all lowercase print(name3.isupper()) #Whether it is all uppercase print(name3.istitle()) #Whether the first letter is uppercase print(name3.isprintable()) #Whether it is a printable character print(name3.isspace ()) #Whether it is a space

3, the string is filled print(name.ljust(30,'-')) #The left side is filled with '-' to the given number of characters print(name.rjust(30,'- ')) #The right fills the left with '-' to reach the given number of characters print(name.zfill(30)) #The right fills the left with '0' to reach the given number of characters

4, the left and right spaces of the string are processed# Remove the given character (beginning or ending) by default it is \t, \r, \n, '' four cases name = 'asdfFGas' print(name.lstrip('as')) #Remove left space print( name.rstrip('as')) #Go to the right space print(name.strip('as' )) #Remove the spaces on the left and right sides

5. String segmentation

print(name.partition('d')) #The string is divided into three parts according to the delimiter and then the pre-cut character print(name.rpartition('d')) #The string is divided into three parts according to the delimiter and after the pre-cut character Three parts

print(name.rsplit('d')) #String split, split into two parts according to the separator, without separator print(name.split('d')) #String split, according to The separator is split into two parts before and after, without separator

 

Guess you like

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