"Learning Python with You Hand in Hand" 9-String Operations

​Through the two articles of "Learning Python by Hand " 7-String Indexing and "Learning Python by Hand" 8-String Slicing , we have mastered the two very important string operations of indexing and slicing method.

In fact, to be more precise, indexing and slicing belong to string operations, [] and [:] are both operators.

So, in addition to indexing and slicing, what other calculation methods are there for strings?

I first list the calculation methods of strings below:

Operator

effect

Instance

+

String concatenation

a + b Output result: HelloWorld

*

Repeat output string

a*2 Output result: HelloHello

[]

String index

a[1] output result e

[ : ]

String slice

a[1:4] output result ell

in

Whether the string contains the specified character

'w' in a outputs False

not in

Whether the string does not contain the specified character

'w' not in a Output result is True

r/R

Raw string

print (r'He \\ llo \ nWo \ trld ')

print (R'He \\ llo \ nWo \ trld ')

%

Formatted output string

Please refer to the following chapters.

The a and b in the third column of the above table are the two variables we are about to assign. Among them, the value of a is "Hello" and the value of b is "World". For the completeness of the table, the examples to be described later are placed on the top, and they will be explained one by one later.

1. String connection

String concatenation in Python is very convenient, just like we do addition, a+b gets ab. Similar operations are limited to addition and multiplication, which will be discussed later. Subtraction and division are not acceptable. . .

In [1]: a = 'Hello'
        b = 'World'
​
In [2]: a + b
Out[2]: 'HelloWorld'
​
In [3]: a + ' ' + b   # a + '空格' + b,变量和字符串可以混用
Out[3]: 'Hello World'

2. Repeat output string

Repeated output string is similar to multiplication, and the symbol used is also *. *The front is the content to be repeated output, and the back is the number of repeated output.

In [4]: a * 3
Out[4]: HelloHelloHello'
​
In [5]: 'yes' * 5   # 不使用变量,直接重复字符串也可以
Out[5]: 'yesyesyesyesyes'
​
In [6]: (a + b) * 3   # 括号内先运算
Out[6]: 'HelloWorldHelloWorldHelloWorld'

3. The index of the string

Please refer to "Learning Python with You Hand in Hand" 7-Index of Strings .

4. Slicing of strings

Please refer to "Learning Python with You Hand in Hand" 8-String Slicing .

5. Whether the string contains the specified characters

Both in and not in described below are member operators. in is to determine whether the string contains the specified character or string. If it does, it returns True; if it does not, it returns FALSE.

True and FALSE are boolean values, which are used to represent the results in comparison operations and judgment statements. True means true or true, False means false or not true.

In [7]: 'w' in a
Out[7]: FALSE
​
In [8]: 'rl' in b
Out[8]: True
​
In [9]: a + b in 'HelloWorldHelloWorld'   # 先进行算数运算,再进行成员运算
Out[9]: True

6. Whether the string does not contain the specified characters

not in is to judge whether the character string "does not" contains the specified character or string. If it does not, it returns True; if it does, it returns FALSE.

In [10]: 'w' not in a
Out[10]: True
​
In [11]: 'rl' not in b
Out[11]: FALSE
​
In [12]: a + b not in 'HelloWorldHelloWorld'   # 先进行算数运算,再进行成员运算
Out[12]: FALSE

7, the original string

In the article "Learning Python with You Hand in Hand" 6-String Identification , we talked about the escape character represented by the backslash \. If in a special case, there are many characters such as \ or \n in the string itself, but you don't want the program to recognize them as transfer characters, how should you do it?

At this time, you need to use this original string identifier. Just put an r or R in front of the string (both upper and lower case). When printing, any character in the string only represents itself, without the function of escaping.

In [13]: print('He\\llo\nWo\trld')
Out[13]: He\llo
         Wo  rld
​
In [14]: print(r'He\\llo\nWo\trld')
Out[14]: He\\llo\nWo\trld
​
In [15]: print(R'He\\llo\nWo\trld')
Out[15]: He\\llo\nWo\trld

8. Format the output string

Formatted output is a very useful application of strings, but it will be a little difficult to understand, and it is not currently available. Therefore, for the formatted output method of strings, we plan to write a separate article in the future, and then contact Everyone share.

Compared with the indexing and slicing of the string, the operation of the string can be said to be very simple, and the next thing to do is to use it flexibly.

In the next article, I will share common functions related to strings with you, so stay tuned.

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" public account, read other articles in the "Learning Python with You Hand in Hand" series, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

"Learning Python with You Hand in Hand" 8-String Slicing

For Fans: Follow the "also said Python" official account, reply "Hand 9", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98756654