Python learning - string manipulation

Common operations of strings include but are not limited to the following:

String replacement, deletion, interception, copy, connection, comparison, search, segmentation, etc
1 #capitalize: String initial uppercase: capitalize-Capitalize the first letter of the string and lowercase other letters
2 name = 'swhthaitun'
3 name.capitalize()
4 print(name.capitalize())
1 #casefold: String all letters lowercase: casefold-Converts all uppercase characters in a string to lowercase characters
2 name = 'HelloWorld'
3 name.casefold()    #Convert characters in all languages to lowercase
4 name.lower()     #lower Only right ASCII Valid, invalid for other languages
5 print(name.casefold())
6 print(name.lower())
1 #center: String width fill, using the original string+Padded characters make up a new string of a specified length
2 name = 'swhthaitun'
3 #Fill the original string with the same number of characters
4 print(name.center(15))
5 print(name.center(16,'*'))
6 name2 = 'HelloWorld'
7 print(name.center(20,'*'))
1 #count: Count the number of times a character appears in the string, or complete the above operations within the specified interval of the string
2 name = 'swhthaitun'
3 result = name.count('h')    
4 print(result)     #2
5 result2 = name.count('h',0,3)
6 print(result2)    #1
7 name2 = 'HelloWorld'
8 result3 = name2.count('W')    #Can not be replaced'w',Python Case sensitive
9 print(result3)    #1
1 #encode: Encoding strings
2 name = 'swhthaitun'
3 name.encode()
4 print(name.encode())    #b'swhthaitun'
1 #endwith: Determines whether a string ends with a character or string, and the return value is a Boolean value
2 name = 'swhthaitun'
3 result1 = name.endswith('s')
4 print(result1)   #False
5 result2 = name.endswith('n')
6 print(result2)   #True
7 result3 = name.endswith('tun')
8 print(result3)   #True
1 #expandtabs: Tab tabs'\t'Convert to tab Key split, default tabsize=8
2 li = 'sw\tht'
3 li.expandtabs(4)
4 print(li.expandtabs(4))     #sw  ht
5 print(li.expandtabs())     #sw      ht
1 #find: Find the specified string in the string, no event returned-1
2 name = 'swht'
3 name.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 output string
2 li = "I'm {},{}"    #among{}Placeholder
3 result = li.format('swht','Welcome to China')
4 print(result)    #I'm swht,Welcome to China
1 #__contains__: Determine whether a character is included in the string, and the return value is Boolean
2 name = 'swhtkkskjj'
3 result = name.__contains__('swht')
4 print(result)    #True
1 #index: Find the specified string in the string, and report an error if it cannot be found
2 name = 'swhthaitun'
3 result1 = name.index('h')
4 #result2 = name.index('m')
5 print(result1)    #2
6 #print(result2)     #There is no 'm' in the string. An exception is reported
1 #join: String splicing
2 name = 'swhthaitun'
3 '*'.join(name)
4 name.join('ab')
5 print(name.join('ab'))    #aswhthaitunb
6 print('*'.join(name))     #s*w*h*t*h*a*i*t*u*n
1 #isalnum: Check to see if the string contains letters or numbers
2 name1 = 'swhthaitun'
3 name2 = '12345'
4 result1 = name1.isalnum()
5 result2 = name2.isalnum()
6 print(result1)    #True
7 print(result2)    #True
1 #isalpha: Check whether the string is completely composed of letters
2 name = 'swhthaitun'
3 result = name.isalpha()
4 print(result)    #True
1 #isdecimal: Check if the string contains only decimal characters
2 name = '123456'
3 result = name.isdecimal()
4 print(result)     #True
1 # isdecimal()
2 # True: Unicode Number,, full angle number (double byte)
3 # False: Roman numeral, Chinese numeral
4 # Error: byte Number (single byte)
1 #isdigit: Check whether the string is only composed of numbers
2 name = '12345'
3 result = name.isdigit()
4 print(result)    #True
1 # isdigit()
2 # True: Unicode Number, byte Number (single byte), full angle number (double byte), Roman number
3 # False: Chinese characters number
4 # Error: nothing
1 #isidentifier: Detect whether the string starts with a letter
2 name = 'swhthaitun'
3 result = name.isidentifier()
4 print(result)   #True
5 
6 name2 = '2swhthaitun'
7 result2 = name2.isidentifier()
8 print(result2)     #False 
1 #isnumeric: To check whether a string is only composed of numbers, this method is only applicable to unicode object
2 name = 'swhthaitun'
3 result = name.isnumeric()
4 print(result)     #False
5 
6 name2 = '123545'
7 result2 = name2.isnumeric()
8 print(result2)    #True  
1 #isprintable: Determine whether all characters in the string belong to visible characters
2 name = '\tPuppy'
3 result = name.isprintable()
4 print(result)      #False
5 
6 name2 = 'swhthaitun'
7 result2 = name2.isprintable()
8 print(result2)      #True   
 1 #isspace: Check whether all strings are spaces
 2 #istitle: Checks whether the initial of each successive string in a string is uppercase
 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: Determine whether all letters in the string are capital letters
10 #lower: Convert all letters to lowercase
11 #lstrip: Remove the leading space to the left of the string
12 #rstrip: Remove spaces at the end of the right side of the string
13 #strip: Remove spaces on both sides of the string
1 #maketrans: The conversion table used to create character mapping. For the call method receiving two parameters, the first parameter is string, which represents the character to be converted
2 #The second parameter indicates the target of the string to be converted
3 intab = 'swhtr'
4 outtab = '12345'
5 name = 'swhtr hjjksknsnjmk'
6 print(name.maketrans(intab,outtab))   #{115: 49, 119: 50, 104: 51, 116: 52, 114: 53}
1 #partition: Splits a string based on the specified delimiter
2 name = 'ls'
3 li = 'hhsslswhtolljm'
4 result = li.partition(name)
5 print(result)     #('hhss', 'ls', 'whtolljm')
 1 ා replace: replace old (old string) in string with new (new string). If the third parameter max is specified, the replacement will not exceed max times
 2 '' syntax: str.replace(old, new[, max])
 3 parameters: old -- substring to be replaced.
 4 new -- a new string to replace the old substring.
 5 Max -- optional string, replacement no more than 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 string
10 print(str_new2)   #thwas was string example.....wow!!! thwas is really string  
1 #split: String split, the default is space,
2 #Syntax: str.split(str="", num=string.count(str)).
3 #str -- Separator, default to all empty characters, including space, line feed(\n),Tab character(\t)And so on. num--Segmentation times
4 name = 'swhthaitun'
5 result = name.split('h')
6 result2 = name.split('h',1)
7 print(result)   #['sw', 't', 'aitun']
8 print(result2)  #['sw', 'thaitun']
1 #__add__: Add the specified character or string after the string
2 name = 'swht'
3 result = name.__add__('e')
4 print(result)     #swhte
5 li = 'hjh'
6 result2 = name.__add__(li)
7 print(result2)     #swhthjh
1 #__eq__: Determine whether the strings are equal. The return 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 (double byte)
#False: Roman numeral, Chinese numeral
#Error: byte number (single byte)

Keywords: Python ascii encoding

Added by TheCase on Mon, 06 Apr 2020 00:19:46 +0300