Hello, I’m Mengya.

The last article covered numeric types in Python data types, and this article takes the turn of another data type heavyweight, strings.

Don’t say a word, start directly, content is a bit much, want to eat slowly.

Understanding strings

The official word for a string is:

A string is a finite string of zero or more characters.

Look said quite foreign style, to put it bluntly is a pile of characters. Our love affair with programming began with “Hello World” as a string.

A string contains more than letters. It can be treated as a string regardless of Chinese or English characters or special characters such as Spaces.

In Python, string types are represented by STR, enclosed in single or double quotation marks in English.

As mentioned in the previous article, numbers can have variables pointing to them, as well as strings that are data types, just like numbers.

String conjunction

If I have two strings, ‘Hello’ and ‘World’, what do I want to do with ‘Hello World’?

This is a string concatenation.

Python provides a particularly easy way to do this, which is to add, which is the addition of addition, subtraction, multiplication and division.

But there’s a limitation: you can only add things of the same type. Such as string to string addition and number to number addition.

One more thing to add here, strings can be multiplied as well as added. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can contact Wei: 762459510, that is really good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~

As for subtraction and division, no.

Escape & unescape characters

Escape character

Let’s start with an example:

Output error, why?

This is because there are three single quotation marks in the first line of the string, and the interpreter is confused as to which two single quotation marks are a pair.

How to solve this problem? There are two solutions.

First: enclose the entire string in double quotation marks.

The second is to use what the title calls “escape characters.”

A backslash solves the problem, “” which means single quotation marks.

From that, we know that escape characters do not take the meaning of the symbol itself, but take a defined meaning.

The following are commonly used escape characters in the figure.

Escape characters are good, but they’re also bad for characters that have lost their meaning.

So how can they be themselves? Find a way to be an unescaped character.

Do not escape characters

In keeping with the previous title, it is a bit inappropriate to use this title as an unescaped character. It is more appropriate to use this title as an original character.

In the commonly used escape characters above, ‘\n’ means newline.

What if someone ‘\n’ just wants to be ‘\n’ and doesn’t want to be a newline?

Simple, add an ‘r’ to the front of the Python string:

Operation string

There are two common ways to manipulate strings in Python. The first is indexing + slicing, and the second is string functions.

Index + slice

First, the index.

The index looks like a foreign name, which is simply a number, like an exam ranking, which all programming languages have.

The index in Python is slightly different from the real ranking in that it starts at 0. To get the first character, use string[0], and so on.

Remember I mentioned above that special Spaces and things like that are also characters, so they’re also numbered, so that’s the forward representation.

In Python, the index of a string can also be represented backwards, starting with -1, -2, -3, and…..

If I want to find a substring, what if I want to find one of the substrings?

And that’s where our slices come in. Cut straight!

I want the string love:

In Python, the function of slicing is left closed and right open. In the string string, the index of L is 2, and the index of e is 5, so to cut love, you need string[2:6], i.e. [2, 6].

One more thing to note here is that slicing the string has no effect on the original string and does not change it.

In addition to this, the slicing operation also has many positions, can cut colorful. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can contact Wei: 762459510, that is really good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~

Of course, these are just a few of them, more slices unlock yourself to explore, the sense of achievement do not do not.

String function

There are many methods in a string, but here are just a few that are commonly used.

find()

Checks if the string contains a particular character and returns the starting index if it does; Otherwise, -1 is returned.

index()

Checks if the string contains the specified character, if so, returns the starting index value, otherwise, an error message is displayed.

count(str1,start,end)

Returns the number of occurrences of [start, end] within the specified index range in string.

replace(str1,count)

Replace str1 in str1 with str2, if count is specified, no more than count times.

Split (‘ split ‘,maxsplit)

MaxSplit defaults to -1, which splits all that can be split according to the delimiter. The return value is a list. If maxSplit has a value, only maxSplit substrings are split.

strip()

Remove whitespace characters from the left and right sides of the string.

I’m just going to write the first three here, but there’s more to strings than that, and you can look at it through dir(STR).

These functions are easy to implement in interactive mode, and you can use the help() function to see which one you want to use, such as the split() function.

You can always watch a few if you don’t mind.

Try it yourself in interactive mode as I did above, so that you can remember when you use a method, otherwise it’s a waste of time to write a lot of code to implement a method that already exists. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can contact Wei: 762459510, that is really good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~

String output

Manipulating strings is necessarily accompanied by the output of strings.

In a real project, the output of a string is definitely not a random, necessarily template output, which involves string formatting.

String formatting is to define a template, leave empty Spaces in one or more places of the template, fill those Spaces with strings that meet the specified criteria, and display the results.

Let’s look at an example:

The ‘%s’ above is what I call an empty space, technically called a placeholder.

The format() method is now more commonly used. Here is how it is used:

In format, that’s a placeholder, and don’t forget that important period between the string and format.