[Introduction to python] String (4)

       This chapter talks about the use of strings. Strings are the most commonly used data type in Python. We can use single quotes ( ' ) or double quotes ( " ) to create strings. Then the next step is Enter this chapter for a study.

1. Environment configuration

My environmentpython is version 3.7.8. The official download path is as follows:

1.python 3.7.8  

You can directly enter the official website to download and install:Download Python | Python.org

2.The compiler chooses spyder, which can be installed through the pip interface:

 
pip install wheel
 
 
pip install PyQt5
 
 
pip install spyder

2. Creation and access of strings

1.Creation of string

Creating a string is actually very simple, just assign a value to a variable, as in the following example:

str1 = 'hello'

str2 = "python"

Everyone should have noticed that we use single quotes when assigning a value to the variable str1, and we use double quotes when assigning a value to the variable str2. In fact, there is no difference between them, and they are both collectively referred to asString.

2. String access

To obtain a string, you can use indexing and then intercept it through square brackets ([]). See the following figure to understand:

example:

str1 = 'hello'

str2 = "python"

str3 = "I Love python" 





print(str1[2])

print(str2[1:3])

print(str3[0:8])

Output:

3. String escape characters "\"

1. When "\" is at the end of a line, it functions as a line continuation.

print("python  \
      C++  \
      C#")

Output:

2. When "\n" is used, it acts as a line break.

print("python\nC++")

Output:

These are the two commonly used escape characters for strings.

4. Common methods of strings

1. lower(): Convert the string content to lowercase

str1 = 'PYTHON'

str2 = str1.casefold()

print(str2)

Output:

2. lower(): Convert the string content to uppercase

str1 = 'python'

str2 = str1.upper()

print(str2)

Output:

There are other methods such as:

encode() encodingstr-->bytes

decode()

decoding
capitalize() Change the first character of the string to uppercase

5. String operators

1. "+"Concatenation of strings

str1 = 'python'

str2 = 'C++'

str3 = str1+str2

print(str3)

Output:

We can try to reverse str1+str2 and output:

2. "*"Repeat output string

str1 = 'python'

print(str1*2)

Output:

3."[]"Index string

String access has been mentioned before.

4. "%"String formatting

4.1 "%s"Format string

print ("我叫 %s" % ('小红'))

Output:

4.2 "%d"Format integer

print ("我叫 %s,今年 %d 岁" % ('小红',15))

Output:

Here I introduce two commonly used string formatting methods.

6. The function of triple quotation marks

1. Triple quotes allow a string to span multiple lines. The string can contain newlines, tabs, and other special characters, which is much more convenient when using MySQL.

example:

str1 = """这是一个多行字符串
第二行字符串。
也可以使用换行符 [ \n ]这是第四行输出!。
"""
print (str1)

 Output:

2. When editing MySQL

cursor.execute('''
CREATE TABLE root (  
name char(20), 
age int,
gender char(20))
''')

This will be much more convenient.

7. Exercises

1. Execute the code below, what is the output?

A. python      

B. never mind

C.  PUT

D. Pyth

2. Execute the code below, what is the output?

A. hello string

B. hellopython

C. string hello

D. str3

 8. Summary

This section about the use of strings ends here. Here we mainly explain some commonly used attributes and methods to facilitate everyone's memory and use. In subsequent chapters, we will continue to talk about the cyclic use of python, thank you!

[Introduction to python] Conditional expressions, loops (5)-CSDN Blog

@Neng

Guess you like

Origin blog.csdn.net/pengneng123/article/details/133993576