(Reminder: The code in this article is run using PyCharm)
(Content is concise, reading time is only a few minutes)
Good afternoon, everyone! I wonder if you have started learning Python?
If you are following my daily updates for learning or using it as review material,
after reading an article, please click the “Looking” button at the bottom of the article!
This way, I will know we are progressing together.
Without further ado, let’s get into today’s topic—strings.
Concept of Strings
Let’s start with the concept. The name of a string may look complex, but it is actually quite simple. Anything we input can be a string; it can be a number, a word, or a sentence.In Python, content surrounded by quotes is a string.
The quotes can be either single quotes or double quotes.
"this is a string"'this is a string'

Modifying the Case of Strings

For example, in the first part, I used all English letters, but sometimes I need to capitalize the first letter of each word or convert everything to uppercase or lowercase. It would be too troublesome to edit it again. We can use some “methods” to achieve this.
|
Method |
Usage |
|---|---|
|
title() |
Capitalize the first letter |
|
upper() |
All uppercase |
|
lower() |
All lowercase |
With the above three methods, we can achieve the goal of changing the case of strings. The specific usage is
When using print(), input the variable name.method name() inside the parentheses.
name = "xiaohu"print(name)print(name.title())print(name.upper())print(name.lower())

Combining Strings

We can use multiple variables to assign different strings to different variables. This way, we can use them separately.
When needed, we can also combine them. To achieve this effect, we need to make good use of the plus sign. This method is also called concatenation.
name_mid = "xiaohu" name_ad = "Uzi" name_top = "bin" team_member = name_top + " " + name_mid + " " + name_ad print(team_member)

Adding Whitespace

Sometimes, we need Python to output blank lines or line breaks. You may find that using print() on different lines sometimes does not achieve this effect.
To ensure correct display, we can use the tab character (\t) and newline character (\n). Note the direction of the slashes; it is not the question mark key.

Removing Whitespace

For us, when we see “xiaohu ” and “xiaohu”, we might think these two strings are the same, but for the program, the former is different from the latter because it has an extra space.
To unify the two, we can use a “method” to remove the whitespace.
|
Method |
Usage |
|---|---|
|
rstrip() |
Remove right whitespace |
|
lstrip() |
Remove left whitespace |
|
strip() |
Remove both sides whitespace |
These three methods are actually easy to remember; they all contain “strip”, with “r” added for the right side and “l” for the left side.
name = " xiaohu "print(name.rstrip())print(name.lstrip())print(name.strip())

Conclusion

That’s all for today! Congratulations on learning another knowledge point.
Please click the “Looking” button below to let me know we are learning together!
Author | Hanko
Images | Internet (please delete if infringing)
Public Account | Python is Easy
Zhihu | Python is Easy