Python 0-1, lecture 2. In the last chapter, we learned variables and how to define a variable. From now on, we can use it directly. In this chapter, let's learn string. Let's have a look.
character string
What is a string? Is it important?
name = 'Hello World' print(name)
'Hello World' here is what we call a string. Name is its variable. The variable name is equal to the string followed by single quotation marks and double quotation marks. Whether it is Chinese characters, letters or numbers, it is a string.
Notes for single and double quotation marks:
#Correct writing name = 'CSDN-"Qing'an"' #Wrong writing name1 = 'CSDN-'Qing'an''
There is also a three quotation mark, which is generally used as a comment. Is there any other use? Yes, let's have a look:
a = """Qing'an security""" print(a) b = 'Qing'an' \ '' \ 'security' print(b) #Printed results: a The results are: Qing'an security b The results are: Qing an an
String can also have many operations. Let's make some changes to the string:
.title()
name = 'hello python' print(name.title())
Here we will get the initial capital string:
Hello Python
.upper()
name = 'hello python' print(name.upper())
Here we will get a string with all uppercase letters:
HELLO PYTHON
. lower() method
As like as two peas, we can guarantee that the whole string is the same as the string we wrote.
name = 'HELLO PYTHON' print(name.lower())
Here we will get the string with all lowercase letters:
hello python
strip() method
Delete spaces: Note: tabs can also be spaces: print('\tpython')
name = 'python ' name = ' python' name = ' python ' #Delete the space at the end print(name.rstrip()) #Delete the space at the beginning print(name.lstrip()) #Remove the spaces on both sides print(name.strip())
The results are:
python python python
Examples
Next, let's use some variables in the string to see an example:
first_name = 'Hello' last_name = 'World' #f is the meaning of string, the abbreviation of format, is a format #Many mistakes can be avoided print(f"{first_name}{last_name}") #You can also write that first_name = 'Hello' last_name = 'World' #Given a variable, full_name = f"{first_name}{last_name}" #Assign values to the first two variables #Then print by the new variable, and the printing effect is the same as above print(full_name)
first_name = 'Qing'an' last_name = 'Nothing else' #You can directly output some Chinese characters or English numbers in it print(f"1,My name is:{first_name}{last_name}!")
The results of these three examples are:
Hello World Hello World There is nothing else to do with peace and security
Value
clear | security | nothing | other | matter | |
positive sequence | 0 | 1 | 2 | 3 | 4 |
Reverse order | -5 | -4 | -3 | -2 | -1 |
name = 'There is nothing else to do with peace and security' print(name[:3])---->Value: Qing'an none #Positive order to the first two print(name[2:4])---->Value: None #positive sequence print(name[-3:-1])---->Value: None #Reverse order print(name[-2: ])---->Value: don't worry #Reverse order print(name[0:5:2])---->Value: nothing #Interval value print(name[::-1])---->Value: nothing else is safe #flashback print(name[:1:-1])---->Value: nothing else #Flashback interval value
It is particularly important to learn to draw inferences from one instance!!!
Other examples
Here are some other operations not mentioned above for understanding. Take a = 'qingan' as an example
operation | Examples | explain |
---|---|---|
division | a.split('g')['qin', 'an'] | Cut the string with 'g' as the dividing point |
replace | a.replace('an ',' an ') qing an | Replace string 'an' with string 'an' |
Splicing | '-'.join(a)q-i-n-g-a-n | Reassemble the whole string with '-' one by one to generate a new string |
format | a = 'qing %s, %d'%('an', 0)qingan,0 | Format operator, it is worth noting that% d can only be an integer |
All of the above results in a new string, not a modified string.
Have you learned this chapter?
If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times!
Finally, basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, advanced test Python programming, Web automation test, APP automation test, interface automation test, advanced continuous integration of test, test architecture development test framework, performance test, security test and other supporting learning resources [free].