String related operations and functions in Python

String related operations

  • string + operation
  • String* operation
  • String[] slice operation
    • String [start value: end value: step value]
      start value: default is 0, end value defaults to the last subscript, step value defaults to 1

Example:

# - String + Operation 
vara = 'You ca  n't see, the water of the Yellow River comes up from the sky, rushing to the sea and can't return.' 
varb = 'You ca  n't see you, the bright mirror is sad and white, the morning is like blue silk and the evening turns into snow' 
# res = vara + varb 
res =  'Wine into' + 'Li Bai' 
# print(res)

# - string * operation 
vars  =  'chicken you are so beautiful,'  *  5  # chicken you are so beautiful you are so beautiful you are so beautiful 
# print(vars)


# - String [] slice operation**** 
# String index operation, only use [] subscript access in the string, can not modify 
'''
0 1 2 3 4 5 6 7 8 9 10 ....
Don't you see, the water of the Yellow River comes from the sky and runs to the sea without returning
                    -4 -3 -2 -1
''' 
vars  =  'You can't see, the water of the Yellow River rises from the sky, and runs to the sea and never returns' 
print ( vars [ 5 ] ) 
print ( vars [ - 5 ] )

'''
String slice operation
str[start value: end value: step value]
Start value: default is 0, end value defaults to the last subscript, step value defaults to 1
''' 
print ( vars ) 
print ( vars [ 5 ] )  # Writing a value is to get the element of the specified subscript 
print ( vars [ 2 : 5 ] )  # Starting from subscript 2, the value can be retrieved until subscript 5 To 2, get less than 5 
print ( vars [ 4 : 8 : 2 ] )  # The water of the Yellow River ==> The Yellow River 
print ( vars [ : : ] )  # From the beginning to the end 
print ( vars [ : : 2 ] ) # From the beginning to the end, take every other 
print ( vars [ : : - 1 ] )   # Reverse the string 
print ( vars [ : : - 2 ] )   # Reverse the string, take every other 
print ( vars [ 1 : : ] )   # No, the water of the Yellow River comes up from the sky, and it flows to the sea and no longer returns 
print ( vars [ 1 : : 2 ] )   # No, on the river, the sea flows again
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

String formatting method

  • format
  • f

Example:

# 1 format Ordinary way 
a =  'Li Bai' 
vars  =  '{}The boat will be about to go, and each other will hear the singing on the shore' . format ( a ) 
vars  =  '{}The boat will be about to go, and each other will hear the shore{}' . format ( a , 'singing song' )

# 2 format Pass the parameters by index 0 1 2 
vars  =  '{2} is about to travel by boat, and hear each other on the shore {1}' . format ( 'a' , 'b' , 'c' )

# 3 The format keyword is passed to the parameter 
vars  =  '{a} will go on a boat, and hear each other on the shore {b}' . format ( a = 'Li Bai' , b = 'singing song' )

# 4 format Container type data parameter 
vars  =  'Bold style: {0[0]}, graceful style: {0[1]}, egg yolk style: {1[0]}, {1[1]}' . format ( [ 'Li Bai' , 'Xin Qiji' ] , [ 'Panpan' , 'Daliyuan' ] )

data =  { 'a' : 'Xin Qiji' , 'b' : 'yolk pie' } 
vars  =  '{a} is about to go on a boat, and hear each other on the shore{b} ' .format ( ** data )

# The new formatting method f method in 3.7 
vars  =  f' { data [ "a" ] } The boat is about to go, and they hear each other on the shore { data [ "b" ] } '
 # print(vars)

# Limit the number of decimal places 
vars  =  'What is the pi: {0:.2f}!My salary is {1:.4f} rmb!' . format ( 3.1415926 , 3242.2342 ) 
# print(vars)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

String related functions

Official documentation: Built-in type: String

https://docs.python.org/3.7/library/stdtypes.html#text-sequence-type-str _ _

(1) English characters and character detection related functions

# String related functions (1) English characters and character detection related functions 
vars  =  'iloveyou' 
# Returns a copy of the string, the first character of the string is uppercase, and the rest are lowercase. 
# str.capitalize() 
res =  vars . capitalize ( ) 
# capitalize the first letter of a word in a string 
res =  vars . title ( ) 
# capitalize the string 
res =  vars . upper ( ) 
# capitalize the character Change all strings to lowercase 
res =  vars . lower ( ) 
# uppercase and lowercase characters in the string, uppercase to lowercase, lowercase to uppercase 
res =  vars . swapcase ( )

# Check if the string consists of all uppercase letters 
res =  vars . isupper ( ) 
# Check if the string consists of all lowercase letters 
res =  vars . islower ( ) 
# Check if the string meets the requirements of the title 
res =  vars . istitle ( ) 
# Check if the string consists of numbers and letters, if the string contains other characters other than numbers and letters, return False 
res =  vars . isalnum ( ) 
# Check whether the string consists of all characters (including English characters and Chinese) composition 
res =  vars . isalpha ( ) 
# Check if the string is composed of pure numeric characters 
res =  vars .isdigit ( ) 
# Check if the current string is composed of space characters ' ' 
res =  vars . isspace ( )

# Check if the string starts with the specified character, you can also specify the start and end positions 
res =  vars . startswith ( 'y' ) 
# res = vars. startswith('y',5) 
# Check whether the string starts with the specified character character ends, you can also specify the start and end positions
 # res = vars.endswith ('y') 
res =  vars.endswith ( ' e' , 1 , 5 )

print ( res )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

(2) Key points of string search and operation related functions

Example:

vars  =  'iloveyou' 
# ** find() method, if found, it returns the index position of the first character that matches the condition. return -1 not found 
res =  vars . find ( 'you' ) 
print ( vars [ res : res + 3 ] )

# index() method 
# res = vars.index('youe') # Return the index position if found, and report ValueError if not found 
# print(res)

vars  =  'user_admin_id_123'

# ** The split() method can split the string into a list according to the specified separator 
# res = vars.split('_') # ['user', 'admin', 'id', '123'] 
res =  vars . split ( '_' , 2 )   # ['user', 'admin', 'id_123'] only splits 2 
# print(res)

# rsplit() method is from right to left, back to front 
# res = vars.rsplit('_') # ['user', 'admin', 'id', '123 ' ] 
res =  vars.rsplit ( '_' , 2 ) # ['user_admin', 'id', '123'] # print(res)  


# ** The join() method, using the specified string, links the elements in a container into a string 
varlist =  [ 'user' ,  'admin' ,  'id' ,  '123' ] 
res =  '_' . join ( varlist ) 
print ( res )

# ** strip() Remove the specified characters from the left and right sides of the string 
vars  =  ' zhangsan ' 
res =  vars . strip ( ' ' ) 
vars  =  '@admin' 
res =  vars . strip ( '@' ) 
# print(vars) 
# print(res)

# rstrip() removes the specified character from the right side of the string, lstrip() removes the specified character from the left side of the string

# ** The len() function can get the length of the current string 
# print(len(vars)) 
# print(len(res))


# ** replace() replace function 
vars  =  'iloveyou' 
# find love and replace it with live 
res =  vars . replace ( 'love' , 'live' )

vars  =  'aabbccddeeabcdef' 
# can limit the number of replacements 
res =  vars . replace ( 'b' , 'B' , 2 ) 
# print(res)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

Tags: String related operations and functions in Python

Topics in Python python

Related: String related operations and functions in Python