Home >System Tutorial >LINUX >Detailed explanation of strings in python
Naming rules for C/C identifiers: Variable names can only contain letters, numbers, and underscores, and cannot start with numbers. C/C keywords and function names cannot be used as variable names.
The rules for variable naming are similar to the naming rules for C/C identifiers: variable names can only contain letters, numbers, and underscores, and cannot start with numbers. You cannot use python keywords and function names as variable names.
In addition, when we choose a name, we try our best to understand the meaning of the name (with a certain degree of descriptiveness).
In python, everything enclosed in quotation marks is a string (can be single quotation mark or double quotation mark)
Although strings can be single quotes or double quotes, if they are mixed, errors may occur, as in the following example:
"I told my friend,'python is really useful' " #true 'I told my friend,"python is really useful" ' #true 'I told my friend,'python is really useful' ' #false
Under normal circumstances, our integrated development environment will highlight the code we write. After writing, you should be able to see the error based on the color (but not everyone can see it). If you don't see it, Come out, you can find the following error when compiling:
SyntaxError: invalid syntax
At this time, we should check whether there are any mixed use of quotation marks in the code.
str = "The best makeup is a smile." print( str ) print( str.title() ) print( str )
The output is as follows:
The best makeup is a smile. The Best Makeup Is A Smile. The best makeup is a smile.
Summary, through this example, it can be seen that the title() method is temporary and does not change the value of the original string.
str = "The best makeup is a smile." print( str ) print( str.upper() ) print( str )
The output is as follows:
The best makeup is a smile. THE BEST MAKEUP IS A SMILE. The best makeup is a smile.
Summary, through this example, it can be seen that the upper() method is temporary and does not change the value of the original string.
str = "The best makeup is a smile." print( str ) print( str.lower() ) print( str )
The output is as follows:
The best makeup is a smile. the best makeup is a smile. The best makeup is a smile.
Summary, through this example, it can be seen that the lower() method is temporary and does not change the value of the original string.
Python uses the " " sign to merge strings.
For example:
str = "The best makeup is a smile." print( "He said that "+str.lower() )
The output is as follows:
He said that the best makeup is a smile.
For example:
str = " The best makeup is a smile." print( str ) print( str.lstrip() ) print( str )
The output is as follows:
The best makeup is a smile. The best makeup is a smile. The best makeup is a smile.
Summary, through this example, it can be seen that the lstrip() method is temporary and does not change the value of the original string.
For example:
str = " The best makeup is a smile. " print( str ) print( str.rstrip() ) print( str )
The output is as follows:
" The best makeup is a smile. " " The best makeup is a smile. " " The best makeup is a smile. "
Summary, through this example, it can be seen that the rstrip() method is temporary and does not change the value of the original string.
例如:
str = " The best makeup is a smile. " print( str ) print( str.strip() ) print( str )
输出如下:
" The best makeup is a smile. " "The best makeup is a smile." " The best makeup is a smile. "
总结,通过这个例子,这可以看出strip()方法是暂时的,并没有更改原来字符串的值。
看到这里,你估计想问,那我如何更改字符串的值呢?只需要将更改过后的值再写回原来的字符串就可以了。
下面我们来举一个例子:
str = "The best makeup is a smile." print( str ) str = str.title() print( str )
输出如下:
The best makeup is a smile. The Best Makeup Is A Smile.
好啦,今天的字符串总结先到这里,如果有疑问,欢迎留言。
The above is the detailed content of Detailed explanation of strings in python. For more information, please follow other related articles on the PHP Chinese website!