python data type _ string common operation of detail
- 2020-06-03 07:02:38
- OfStack
This article mainly introduces the common operation methods and examples of string
1. python string
There are three common ways to declare a string in python: to enclose it in single, double, or triple quotes, as follows:
name = 'hello'
name1 = "hello bei jing "
name2 = '''hello shang hai haha'''
Once the string 1 in python is declared, it cannot be changed, as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
python string common built-in method, the string operation, as follows:
in method:
#in methods : Determines whether the content exists in the string
name = 'hello bei jing zao an '
print('hello' in name) # judge hello If there is a name In the , The execution result is True
print('shang hai ' in name) # judge shanghai If there is a name In the , The execution result is False
not method:
name = 'hello bei jing zao an ' # Define string
#not Method: Returns the result of an expression ' On the contrary value ' . Returns if the result of the expression is true False
print('hello' not in name) # judge hello There is no name , the execution result is False
print('abc' not in name) # judge abc There is no name , the execution result is True
is method:
#is Judgment is Whether the memory address is the same
a = 'xiaoming'
b = 'xiaoming'
print(a is b ) #a and b The same value, memory storage, pointing to the same memory address, pointing to the result is True
print(id(a))
print(id(b)) # Can be achieved by id( The variable name ) This method looks at the memory address of the variable
The string value operation method is as follows:
names = 'hello bei jing one day'
print(names[0:10]) # 1. The value can be evaluated by subscript, and the section can be sliced without tails. The execution result is as follows: hello bei
for name in names:
print(name) #2. through for Loop value. Loop is a loop object (names) The inside of the each 1 An element
for k in range(len(names)):
print(names[k]) #3. cycle names Take the value of, k The value of is a number and corresponds to the subscript of a string
endswith, isalnum, isalpha methods of strings:
name = 'hello world is world'
print(name.endswith('d')) # Judge whether u At the end , The result of the execution is a Boolean. In the working example, you can determine whether the image is a Boolean jpg At the end
print('ab123'.isalnum()) # Can be used to determine if the input string contains Numbers and letters, or if the password contains Numbers and letters, and returns a Boolean value
print('abcdA'.isalpha()) # Determines whether the input string is an English letter and returns a Boolean value
Determine if the input string is a number, as follows:
name = ' abcdERF123'
print('123'.isdigit()) # Determines whether the input string is a number and returns a Boolean value
Remove the whitespace from the string as follows:
# Remove the blank space
print(' ab fs'.lstrip()) # By default, Spaces and newlines to the left of the string are removed , Execution Results: ab fs
print('hello '.rstrip()) # By default, Spaces and newlines on the right side of the string are removed to execute the result: hello
print('\nmysql abcd'.strip()) # By default, both Spaces and newlines are removed , Execution Results: mysql abcd The space in the middle cannot be removed
print('mysqlmy'.strip('m')) # Removes the specified string, for example: removes both sides of the string m The element , Execution Results: ysqlm y
The join method of the string, as follows:
#join Is used to pass through Some string Joining together 1 Each element of a iterable object --->join( Iterable object parameter types )
print('*'.join(name)) # Use each element in the string * No. Connection, execution result: a*b*c*d*E*R*F*1*2*3 To return to 1 Three new variable values
print(' use * After the number is spliced name Value: ', name) #name The content of the string has not been changed. The execution result is: abcdERF123
nums = ['ybq', 'lhl', 'lsh']
print(' . '.join(nums)) # Converts the list to a string, with commas between each element, and the result is: ybq . lhl . lsh
# On the other 1 Method to convert a list to a string
# nums = ['ybq', 'lhl', 'lsh']
# temp = ''
# for i in nums:
# temp = temp+i+',' # The list is converted to a string, the type is cast, and the execution result is: ybq . lhl . lsh
# print(temp.strip(','))
Replace the replace string as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
0
Find the find string as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
1
Cut the string spilt and return the result type list, as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
2
It is not commonly used to split strings according to newline characters, as follows:
print('1+2+3\n1+3+4'.splitlines()) # Split by line break , I'm dividing by each 1 Line file contents as list the 1 Element, the execution result is: ['1+2+3', '1+3+4']
The string generates upper and lower case letters and Numbers randomly, which can be used as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
4
Format string, as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
5
String is not commonly used methods, just know:
The judgment and conversion of upper and lower case letters in the string are as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
6
String mapping can be used for password encryption, as follows:
# The string is immutable, meaning that it cannot pass a test on a 1 Position reassignment changes the content
name = 'hello'
name[0] = 'k' # Modify the value of the string by subscript to report an error message: TypeError: 'str' object does not support item assignment
7
Capitalize the first letter of the string
name = 'hello world is world'
print(name.capitalize()) # Capital letter, execution result: Hello world
print(name.center(50, '*')) # The total length is 50 That will be name The value of the string is placed in the middle and supplemented on both sides * Display of
Welcome to beef supplement ~~~