Python learning entry to the master: the concept of strings

Python learning entry to the master: the concept of strings

1. Creation of string

String is the most commonly used data type in python. We use it to process text content. The following code demonstrates the method of creating a string in 3

str_1 ='python'
str_2 = "python"
str_3 = """python"""

print(str_1, type(str_1))
print(str_2, type(str_2))
print(str_3, type(str_3))

Create a new script in pycharm. If this tutorial requires you to write code in an interactive interpreter, the default is to write code in pycharm.

In the above code, I used single quotes, double quotes, and 3 pairs of double quotes to create a string, and use the print function to output the contents of these 3 variables and their types. The actual output is

python <class'str'>
python <class'str'>
python <class'str'>

Among the three methods for creating strings, the first two are commonly used. When using the print function, multiple data can be separated by commas.

Multiple strings can be connected together using + to generate a new string, follow me in the interactive interpreter

>>> a ='I'
>>> b ='like'
>>> c ='python'
>>> d = a + '' + b + '' + c
>>> d
'I like python'
>>> len(d)
13

I defined three variables, the types of which are all strings, and finally use + to connect them together. During the connection, in order to prevent the words from being close together, I added '' in the middle, which is also a string. The content is a space.

Using the built-in function len(), you can get the length of the string, and you have learned a new built-in function.

2. The concept of index

This is the first very important professional concept you come into contact with when you are learning a programming language. You will use it later when you learn about lists. If you don't understand the index, you can't learn slices.

In Python, a string is an ordered collection of characters. Here, you are mainly concerned with the word ordered.

'python'
'443'
'*&^%$'

There are 5 letters in the first string, 3 numbers in the second string, and 5 characters in the third string. In programming languages, these are collectively called characters. Therefore, strings are characters. Collection.

So how is order manifested? Order means that each character has its own position. The technical term is called index, such as the string'python'. I now ask you what is the third character of this string. You will answer that it is the character t Counting from left to right, it is indeed t, this is the index.

But unlike the familiar counting method, in programming languages, the index starts from 0, so the index of t in the string python is 2

Not only that, but python also supports inverted index, the inverted index of t is -4.

You can access a character in the string by index, follow me in the editor

>>> a ='python'
>>> a[0]
'p'
>>> a[-2]
'o'
>>> a[1:3]
'yt'
>>> a[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

When accessing a character in a string according to the index, you need to use a pair of square brackets [] to fill in the index. If the index exceeds the range, an IndexError will be raised, which is a very easy mistake for beginners.

a[1:3] represents the characters in the range index 1 to index 3. The result is'yt', which is the slice operation, and there will be a special article on the slice operation.

3. Operation of strings

For strings, there are the following operations

The string concatenation operation has already been explained when introducing the concept of string. Let’s follow me in the interactive interpreter to learn these operators

>>> a ='py'
>>> b ='python'
>>> a + b
'pypython'
>>> a*3
'pypypy'
>>> b[3]
'h'
>>> b[0:2]
'py'
>>> a in b
True
>>> b not in b
False

There will be a special article to explain about%, format string.

Reference: https://cloud.tencent.com/developer/article/1582929 Python learning from entry to proficiency: the concept of strings-Cloud + Community-Tencent Cloud