Python learning-string operations, python learning strings
Common Operations on strings include but are not limited to the following:
String replacement, deletion, interception, replication, connection, comparison, search, and Segmentation
1 #Capitalize: Uppercase character string: capitalize-capital the first character of the character string. Other letters: lowercase 2 name = 'swhthaitun '3 name. capitalize () 4 print (name. capitalize ())
1 #Casefold: Lowercase letters of all strings: casefold-convert all uppercase characters in the string to lowercase characters 2 name = 'helloworld' 3 name. casefold () # convert the characters in all languages to lowercase 4 name. lower () # lower is valid only for ASCII and 5 (name. casefold () 6 print (name. lower ())
1 #Center: String width filling. The new string with the specified length is formed using the original string + filling character. 2 name = 'swhthaitun' 3 # Fill the same number of characters at both ends of the original string 4 print (name. center (15) 5 print (name. center (16, '*') 6 name2 = 'helloworld' 7 print (name. center (20 ,'*'))
1 #Count: Counts the number of times a character appears in a string, or completes the preceding operation within the specified range of the string. 2 name = 'swhthaitun '3 result = name. count ('H') 4 print (result) #25 result2 = name. count ('h',) 6 print (result2) #17 name2 = 'helloworld' 8 result3 = name2.count ('W') # cannot be changed to 'w ', python is case sensitive 9 print (result3) #1
1 #Encode: Encode the string 2 name = 'swhthaitun '3 name. encode () 4 print (name. encode () # B' swhthaitun'
1 #Endwith: Determines whether the string ends with a certain character or string. The returned value is a Boolean value of 2 name = 'swhthaitun '3 result1 = name. endswith ('s ') 4 print (result1) # False5 result2 = name. endswith ('n') 6 print (result2) # True7 result3 = name. endswith ('T') 8 print (result3) # True
1 #Expandtabs: Convert the tab '\ t' to the tab key with the specified width. The default tabsize is 82 li = 'sw \ tht' 3 li. expandtabs (4) 4 print (li. expandtabs (4) # sw ht5 print (li. expandtabs () # sw ht
1 #Find: Search for the specified string in the string. If no result is found,-12 name = 'swht '3 name is returned. find ('s ') 4 name. find ('H') 5 name. find ('A') 6 print (name. find ('s '), name. find ('H'), name. find ('A') #0 2-1
1 #Format: Format the output string 2 li = "I'm {},{}" # Where {} is the placeholder 3 result = li. format ('swht ', 'Welcome to China') 4 print (result) # I'm swht, welcome to China
1 #_ Contains __: Determines whether a string contains certain characters. The returned value is a Boolean value of 2 name = 'swhtkskjj '3 result = name. _ contains _ ('swht ') 4 print (result) # True
1 #Index: Find the specified string in the string. If the string cannot be found, the system returns the 2 name = 'swhthaitun '3 result1 = name. index ('H') 4 # result2 = name. index ('M') 5 print (result1) #26 # print (result2) # No 'M' in the string. An error is returned.
1 #Join: String concatenation 2 name = 'swhthaitun '3 '*'. join (name) 4 name. join ('AB') 5 print (name. join ('AB') # aswhthaitunb6 print ('*'. join (name) # s * w * h * t * h * a * I * t * u * n
1 #Isalnum: Check whether the string contains letters or numbers 2 name1 = 'swthaitun '3 name2 = '000000' 4 result1 = name1.isalnum () 5 result2 = name2.isalnum () 6 print (result1) # True7 print (result2) # True
1 #Isalpha: Checks whether the string is completely composed of letters. 2 name = 'swhthaitun '3 result = name. isalpha () 4 print (result) # True
1 #Isdecimal: Check whether the string contains only the decimal character 2 name = '000000' 3 result = name. isdecimal () 4 print (result) # True
1 # isdecimal () 2 # True: Unicode number, full-angle number (double byte) 3 # False: Roman numerals, Chinese numerals 4 # Error: byte digit (single byte)
1 #Isdigit: Check whether the string contains only numbers. 2 name = '000000' 3 result = name. isdigit () 4 print (result) # True
1 # isdigit () 2 # True: Unicode number, byte number (single byte), full-byte number (double byte), Rome number 3 # False: Chinese Character number 4 # Error: None
1 #Isidentifier: Check whether the string starts with 2 name = 'swhthaitun '3 result = name. isidentifier () 4 print (result) # True5 6 name2 = '2swhthaitun '7 result2 = name2.isidentifier () 8 print (result2) # False
1 #Isnumeric: Checks whether the string is composed of digits. This method is only applicable to unicode object 2 name = 'swhthaitun '3 result = name. isnumeric () 4 print (result) # False5 6 name2 = '000000' 7 result2 = name2.isnumeric () 8 print (result2) # True
1 #Isprintable: Determines whether all characters in the string belong to the visible character 2 name = '\ tPuppy' 3 result = name. isprintable () 4 print (result) # False5 6 name2 = 'swhthaitun '7 result2 = name2.isprintable () 8 print (result2) # True
1 # isspace: Check whether all strings are blank. 2 # istitle: checks whether the first letter of each consecutive string is capitalized. 3 # name = 'puppy '# True 4 # name = 'puppy abc' # True 5 # name = 'puppy' # True 6 # name = 'a Puppy '# False 7 # print (name. istitle () 8 9 # isupper: determines whether all letters in the string are uppercase letters 10 # lower: converts all letters to lowercase letters 11 # lstrip: remove spaces starting with the left of the string 12 # rstrip: Remove spaces ending with the right of the string 13 # strip: Remove spaces on both sides of the string
1 #Maketrans: Used to create a conversion table for character ing. For the call method that receives two parameters, the first parameter is a string, represents the character 2 to be converted # The second parameter represents the target to be converted by the string (not easy to understand) 3 intab = 'swhtr' 4 outtab = '000000' 5 name = 'swhtr hjjksknsnjmk '6 print (name. maketrans (intab, outtab) # {115: 49,119: 50,104: 51,116: 52,114: 53}
1 #Partition: Splits the string according to the specified separator. 2 name = 'LS' 3 li = 'hhsslswhtolljm '4 result = li. partition (name) 5 print (result) # ('hws', 'Ls', 'whtolljm ')
1 #Replace: Replace the old (old string) in the string with new (new String). If the third parameter max is specified, replace it with the 2 ''' Syntax: str. replace (old, new [, max]) 3 parameter: old -- the substring to be replaced. 4 new -- new String, used to replace the old substring. 5 max -- Optional string, cannot exceed max times '''6 str = 'this is string example ...... wow !!! This is really string '7 str_new = str. replace ('is', 'was') 8 str_new2 = str. replace ('is', 'was', 3) 9 print (str_new) # thwas was string example ..... wow !!! Thwas was really string10 print (str_new2) # thwas was string example ...... wow !!! Thwas is really string
1 #Split: String segmentation. The default value is space. 2 # Syntax: str. split (str = "", num = string. count (str )). 3 # str -- delimiter, which defaults to all null characters, including spaces, line breaks (\ n), and tabs (\ t. Num -- number of splits 4 name = 'swhthaitun '5 result = name. split ('H') 6 result2 = name. split ('h', 1) 7 print (result) # ['sw ', 't', 'aitun'] 8 print (result2) # ['sw ', 'thaiton']
1 #_ Add __: Add the specified character or string 2 name = 'swht '3 result = name after the string. _ add _ ('E') 4 print (result) # swhte5 li = 'hsf-'6 result2 = name. _ add _ (li) 7 print (result2) # swhthjh
1 #_ Eq __: Determines whether the string is equal. The returned value is Boolean 2 name = 'swht '3 li = 'test' 4 result = name. _ eq _ (li) 5 print (result) # False
# Isdecimal ()
# True: Unicode number, full-angle number (dual-byte)
# False: Roman numerals, numbers of Chinese Characters
# Error: byte number (single byte)