Home > System Tutorial > LINUX > body text

Detailed explanation of strings in python

王林
Release: 2024-02-14 17:30:30
forward
941 people have browsed it

Detailed explanation of strings in python

1, variable naming

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).

2.python string

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
Copy after login

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
Copy after login

At this time, we should check whether there are any mixed use of quotation marks in the code.

3, Summary of string methods
(1)The title() method of capitalizing the first letter of each word
str = "The best makeup is a smile."
print( str )
print( str.title() )
print( str )
Copy after login

The output is as follows:

The best makeup is a smile.
The Best Makeup Is A Smile.
The best makeup is a smile.
Copy after login

Summary, through this example, it can be seen that the title() method is temporary and does not change the value of the original string.

(2) The upper() method that changes the string to all uppercase letters
str = "The best makeup is a smile."
print( str )
print( str.upper() )
print( str )
Copy after login

The output is as follows:

The best makeup is a smile.
THE BEST MAKEUP IS A SMILE.
The best makeup is a smile.
Copy after login

Summary, through this example, it can be seen that the upper() method is temporary and does not change the value of the original string.

(3) The lower() method that changes the string to all lowercase
str = "The best makeup is a smile."
print( str )
print( str.lower() )
print( str )
Copy after login

The output is as follows:

The best makeup is a smile.
the best makeup is a smile.
The best makeup is a smile.
Copy after login

Summary, through this example, it can be seen that the lower() method is temporary and does not change the value of the original string.

(4) Combine strings

Python uses the " " sign to merge strings.
For example:

str = "The best makeup is a smile."
print( "He said that "+str.lower() )
Copy after login

The output is as follows:

He said that the best makeup is a smile.
Copy after login
(5) lstrip() method to delete the blank space at the front of the string

For example:

str = "    The best makeup is a smile."
print( str )
print( str.lstrip() )
print( str )
Copy after login

The output is as follows:

    The best makeup is a smile.
The best makeup is a smile.
The best makeup is a smile.
Copy after login

Summary, through this example, it can be seen that the lstrip() method is temporary and does not change the value of the original string.

(6) rstrip() method to delete the blank space at the back end of the string

For example:

str = "    The best makeup is a smile.    "
print( str )
print( str.rstrip() )
print( str )
Copy after login

The output is as follows:

"    The best makeup is a smile.    "
"    The best makeup is a smile. "
"    The best makeup is a smile.    "
Copy after login

Summary, through this example, it can be seen that the rstrip() method is temporary and does not change the value of the original string.

(7) Strip() method to delete blanks at both ends of a string

例如:

str = "    The best makeup is a smile.    "
print( str )
print( str.strip() )
print( str )
Copy after login

输出如下:

" The best makeup is a smile. "
"The best makeup is a smile."
" The best makeup is a smile. "
Copy after login

总结,通过这个例子,这可以看出strip()方法是暂时的,并没有更改原来字符串的值。

看到这里,你估计想问,那我如何更改字符串的值呢?只需要将更改过后的值再写回原来的字符串就可以了。

下面我们来举一个例子:

str = "The best makeup is a smile."
print( str )
str = str.title()
print( str )
Copy after login

输出如下:

The best makeup is a smile.
The Best Makeup Is A Smile.
Copy after login

好啦,今天的字符串总结先到这里,如果有疑问,欢迎留言。

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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!