Python quick start series -- string -- detailed explanation

Posted by sarakmo on Mon, 07 Mar 2022 07:40:10 +0100

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'

'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


b = 'Qing'an' \
    '' \

#Printed results:
a The results are:

b The results are:
Qing an an

String can also have many operations. Let's make some changes to the string:


name = 'hello python'

Here we will get the initial capital string:

Hello Python


name = 'hello python'

Here we will get a string with all uppercase letters:


. lower() method

As like as two peas, we can guarantee that the whole string is the same as the string we wrote.


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
#Delete the space at the beginning
#Remove the spaces on both sides

The results are:



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

#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
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


positive sequence01234
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

divisiona.split('g')['qin', 'an']Cut the string with 'g' as the dividing point
replacea.replace('an ',' an ') qing anReplace string 'an' with string 'an'
Splicing'-'.join(a)q-i-n-g-a-nReassemble the whole string with '-' one by one to generate a new string
formata = 'qing %s, %d'%('an', 0)qingan,0Format 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].

Topics: Python software testing Testing