Python used to be so simple

Python used to be so simple (5)

Introduction to Python string data types and various operations (splicing, intercepting and escaping characters)

A large part of the operations in Python are related to string operations. Whether it is data acquisition, data processing, and finally to the presentation of data, it is inseparable from the basic data type of string.

Moreover, python has perfect string operations, some of which are well encapsulated in Python. At the same time, there are various third-party packages on the Internet, which can be said to have everything. Many processes do not need to be written by themselves, which is commonly known as "repeated wheel building". In many practical applications, avoiding repeated wheel building is the performance of improving productivity. After all, if not the deep-rooted programmers, most users only use Python as a tool to improve productivity, so it is not necessary to write it again.

This article will focus on the basic use of strings, and then introduce some third-party packages of strings.

1, Introduction

String literally means that it is composed of multiple single strings. Whether it is composed of a single character or multiple characters, it will be interpreted as a string in Python.

There are two forms of Python string. The final expression results of the two forms are the same, and there is no need to distinguish the forms separately as some become languages. Of course, all symbols in programming are written in English, and Chinese and English punctuation symbols are not interlinked, that is to say, Python interpreter will directly report errors when encountering Chinese punctuation symbols.

1. Manifestation:

  1. Use double quotation marks
  2. In single quotation marks
  3. For multi line text paragraphs, three single quotation marks' '(or three double quotation marks'') can be added before and after the text,
    Note: because there are multiple lines of text here, there will be carriage return and line feed many times. If the printed multiple lines of text do not need to be displayed in multiple lines, but are displayed in one sentence, at this time, you can add \ atthe end of each line, so that the Python interpreter will not interpret the line feed character.

2. Examples:

Here, use the type() function to judge the type of the variable. You can see from the last printed one,
All are < class' str '>, that is, string types.

Example 1:

a = 'This is a string enclosed in single quotes!'
print(a)
print(type(a))

# Output > > >
This is a string enclosed in single quotes!
<class 'str'>

Example 2:

b = "This is a string enclosed in double quotes! Can have number: 3"
print(b)
print(type(b))

# Output > > >
This is a string enclosed in double quotes!
<class 'str'>

Example 3:

c = '''
	This is a string of multiline text strings,
	You can freely add multiple lines of text,
	When printing out, each line will interpret the newline character,
	So what is printed on the interface is what it looks like.
	'''
print(c)
print(type(c))

# Output > > >
This is a string of multiline text strings,
You can add multiple lines of text freely,
When printing out, each line will interpret the newline character,
So what is printed on the interface is what it looks like.
<class 'str'>

Example 4:

d = """
	This is a string of multiline text enclosed in double quotation marks,
	It's the same as enclosed in single quotes.
	"""
print(d)
print(type(d))

# Output > > >
This is a string of multiline text enclosed in double quotation marks,
It's the same as enclosed in single quotes.
<class 'str'>

Example 5:

e = '''
This is a sentence\
Python When compiling, it will directly\
Newline ignored
'''
print(e)
print(type(e))

# Output > > >
This is a sentence Python The newline character will be ignored when compiling
<class 'str'>


2, Escape character

Sometimes we need to add single quotation marks or double quotation marks to a string. If we add them directly, the Python interpreter will directly report an error, so we need the help of escape characters.

Let's take a look at several error examples. In the error example, the wrong use of quotation marks will cause the interpreter to report an error directly.

From the error message of the following example, the interpreter directly outputs an invalid syntax, that is, the nonstandard use of Python syntax, and directly points out the position of the wrong reference single quotation mark. According to the error prompted, we can directly find the location of the error and how to modify it. Of course, because the interpreter interprets line by line, the error in the second line of code below cannot be reported directly. The second error can be found only after the first error is solved.

1. Error examples

# Here, if you add single quotation marks and double quotation marks directly to the string, an error will be reported directly during compilation

print('This is a string with single quotes'character string')
print("Here is another string with double quotes"character string")


# Output > > >
File "d:\MyProject\python-test\test.py", line 1
print('This is a string with single quotation marks'character string')
                        ^
SyntaxError: invalid syntax

2. Correct example

Let's take a look at the correct usage. After using the escape character to escape the quotation marks in the string, the interpreter will not directly regard the quotation marks as the representation symbols of the string, and the content can be output normally.

print('This is a string with single quotes\'character string')
print("Here is another string with double quotes\"character string")

# Output > > >
This is a string with single quotes'character string
 Here is another string with double quotation marks""character string

3. Other usage of escape

Of course, escape symbols are not only used for the escape of quotation marks, but also for the escape of other characters, which can be used under various special conditions.

Escape characterdescribeexampleoutput
\aRing the bellprint('\a')
\bBackspaceprint('Pyt \bhon')Python
\000emptyprint('Py\000thon')Python
\nLine feedprint('Py\nthon')
Py
thon
\vVertical tabprint('Py\vthon')
Py
thon
\thorizontal tab print('Py\tthon')Py thon
\renterprint('Py\rthon')thon
\fPage changeprint('Py\fthon')Py
thon
\yyyOctal number, y represents characters from 0 to 7print("\123")S
\xyyHexadecimal number, which starts with \ x and y represents the characterprint("\x23")#
\otherOther characters are output in normal format



3, String operation

After we have a basic understanding of strings, we can do a series of operations on strings. Here, you will find that the operation of strings in Python is very humanized, not only the splicing and interception of simple strings, but also the operation can be carried out in a way similar to mathematics.

1. String splicing

String splicing mainly uses two operators + and *.
1. Use * to multiply the string like multiplication. Example: 3 * "Python"
2. Use + to splice two independent strings. Example: "Hello" + "World"
3. You can also add nothing between two strings. At this time, the interpreter will automatically splice these two separate strings. Sometimes it is easier to use when the string is long. Example: "Hello" "World"

print(3 * "Python" + "num")
# Output: python python python num

print("Py""thon")
# Output: Python

a = "Hello"
print(a + " World")
# Output: HelloWorld

2. String interception

There is also a lot of string. String interception, also known as string slicing, is widely used. This method is needed when obtaining a substring in a string or a character.

String interception mainly uses two operators [] and [:].
1. Because the string is composed of multiple characters, it can be regarded as a group of lists. At this time, the subscript value can be used to obtain a single character in the string. For example, a = "Python", the output of a[0] is P, because in general programming, the subscript value starts from 0, unless it is specially stated that it starts from 1. Because the subscript can also use a negative number, which means that it can be intercepted from the end to the front. At this time, it can be intercepted flexibly anywhere you want. Sometimes you will encounter a very long string. If you need to take the last few characters at this time, you can use to intercept from the end of the character string, and the intercepted string can also be spliced with the normal string.

Subscript reference >>>
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
a = "Python"

# Gets the character with subscript 2
print(a[2])

# Gets the character with subscript - 2
print(a[-2])

# Get the characters with subscript 2 and splice them
print(a[2] + " Hello World")

# Output:
t
o
t Hello World

2. You can also use [:] to intercept a character in the middle of a string, which is actually the same as the above interception, but you can intercept a string in the middle separately. Compared with intercepting a large section at one time, this judgment of position is more flexible.

a = "Python"

# Intercept strings with 0 ~ 2 subscripts (excluding those with 2 subscripts)
print(a[:2])

# Intercept strings with 0 ~ 2 subscripts (excluding those with 2 subscripts)
print(a[0:2])

# Intercepts the string from 3 to the last subscript of the string
print(a[3:])

# Intercept the string from the end number subscript 2 to the last position of the string (- the subscript of 2 is not included)
print(a[-2:])

# Intercept strings with subscripts from 0 to - 2 (- 2 subscripts are not included)
print(a[:-2])

# Intercept the string and splice it
print(a[-2:] + " Hello World")

# Output:
Py
Py
hon
on
Pyth
on Hello World

3. Of course, if there is a subscript, it may exceed the subscript. At this time, we will encounter the classic subscript out of bounds error.
The following is a Python string as an example. Because the string has only 6 characters, the critical subscript should be 5. At this time, if I fill in 9, I will report an error that the subscript is out of bounds.

a = "Python"
print(a[9])

# Output:
IndexError: string index out of range

4, Summary

Here is a brief introduction to Python strings, the corresponding operations on strings, and some simple usage of escape characters in strings. After that, we will encounter various scenarios of string operation, and the function of string interception will appear in various situations. Therefore, we need to know more about it here.

Of course, here is just a brief mention of string operation. Later, a special article will introduce Python string formatting.

Keywords: Python Programming

Added by fredfish666 on Sun, 23 Jan 2022 23:01:18 +0200