python3 String Operation Summary

  • 2021-07-26 08:10:12
  • OfStack

Introduce the common string processing methods in Python

String interception


>>>s = 'hello'
>>>s[0:3]
'he' 
>>>s[:] # Intercept all characters 
'hello'
 

Eliminate spaces and special symbols


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 

For example:


>>>s = '000hello00world000'
>>>s.strip('0')
'hello00world'
s.strip('12') Equivalent to s.strip('21')

For example:


>>>s = '12hello21'
>>>s.strip('12')
'hello'

The usage of lstrip and rstrip is similar to that of strip, which are used to eliminate left and right characters respectively

String copy


s1 = 'hello'
s2 = s1 # s2 = 'hello'

If the length is specified


s1 = 'hello'
s2 = s1[0:2] #s2 = 'he'

String concatenation


s1 = 'hello'
s2 = 'world'
s3 = s1 + s2 #s3 = 'helloworld'

Or


import operator
s3 = operator.concat(s1,s2) #concat Is a string splicing function 

String comparison

(1) Comparison using operator module method (python3.X cancels cmd function)

The methods included are:

lt (a, b)--Less than le (a, b)--Less than or equal to eq (a, b)--is equal to ne (a, b)--Not equal to ge (a, b)--Greater than or equal to gt (a, b)--Greater than

Examples:


>>>import operator
>>>operator.eq('abc','edf') # According to ASCII Code comparison 
Flase
>>>operator.gt('abc','ab')
True

(2) Relational operator comparison ( > , < , > =, < =, = =,! =)


>>>s1 = 'abc'
>>>s2 = 'ab'
>>>s1 > s2
True
>>>s1 == s2
False

Find the length of string


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
0

Find the largest character and the smallest character in the string


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
1

String case conversion

There are mainly the following methods:

upper--Convert to uppercase lower--Convert to lowercase title--Convert to title (capitalize the first letter of each word) capitalize-First Capital swapcase--Upper case to lower case, lower case to upper case

Examples:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
2

String flip


>>>s1 = 'hello'
>>>s1[::-1]
'olleh'

String segmentation

split method, divides according to parameter, returns 1 list

Examples:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
4

String sequence connection

join method:

The syntax is str. join (seq) # seq is a sequence of elements

Examples:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
5

Intra-string lookup

find method:

Detects whether a string contains a substring str

The syntax is:

str. find (str [, start, end]) # str is the string to look up; strat is to find the starting position, and the default is 0; end is the end of the lookup and defaults to the string length. Returns the starting position index if found, otherwise returns-1

Examples:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
6

Intra-string substitution

replace method:

Replace old strings with new strings in a string

The syntax is:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
7

Examples:


>>>s1 = 'today is a find day'
>>>s1.replace('find','rainy')
'today is a rainy day'

Determine string composition

There are mainly the following methods:

isdigit--Only numbers when detecting strings isalnum--Detects whether a string consists only of numbers and letters isalpha--Detects whether a string consists only of letters islower--Detects if a string contains only lowercase letters isupper--Detects if a string contains only uppercase letters isspace--Detects whether a string contains only spaces istitle--Detects whether a string is a title (capitalize the first letter of each word)

Examples:


s.strip() # Erase string s White space characters on the left and right (including '\t' , '\n','\r','' ) 
 
s.strip('0') # Erase string s Special characters on the left and right, such as '0' ) , In the middle of the string '0' Will not be deleted 
9

String to array


a = 'My name is Jason'
# Use split(str="", num=string.count(str))  Method can be turned according to different dividers, and the number of dividers can also be specified. You can use  ' '.join Method reversal 
>>> 'My name is Jason'.split(' ')
['My', 'name', 'is', 'Jason']
>>> ' '.join(['My', 'name', 'is', 'Jason'])
'My name is Jason'

String end-to-end matching


>>> 'cat.jpg'.startswith('cat')
True
>>> 'cat.jpg'.startswith('cat',0,3)
True
>>> 'cat.jpg'.endswith('.jpg')
True
>>> 'cat.jpg'.endswith('.jpg',-4)
True

String space processing


>>> s = ' Hello World  '
>>> s.strip()
'Hello World'
>>> s.lstrip()
'Hello World  '
>>> s.rstrip()
' Hello World'
# Expand 
>>> 'www.example.com'.lstrip('www.')
'example.com'
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

String formatting, number and case judgment, length completion


>>>s = '000hello00world000'
>>>s.strip('0')
'hello00world'
s.strip('12') Equivalent to s.strip('21')
3

Related articles: