A string is a series of characters. In Python, a string is enclosed in quotation marks, where the quotation marks can be single quotes or double quotes.
For example:
>>> ' This is a string '
' This is a string '
>>> "This is also a string"
"This is also a string"
Operations on Strings:
#修改字符串的大小写 #
1. Modify the first character of the word
>>> name= ' Alben xue '
>>> type (name)
<class ' str ' >
>>> print (Name.title ())
Alben Xue
The title () follows the variable, using the. Connection, representing the variable name in the method defined by the title ()
2, modify all characters
>>> Name
' Alben Xue '
>>> print (Name.upper ())
Alben XUE
>>> print (Name.lower ())
Alben Xue
#合并字符串 #
In Python, string merging with "+"
Example:
>>> first_name= ' Xue '
>>> last_name= ' lingming '
>>> Full_name=first_name+last_name
>>> message= ' Hello, ' +full_name.upper () + '! '
>>> Print (message)
hello,xuelingming!
"The code above, storing a string of strings in a variable, all subsequent operations on the variable execution, this will be more convenient"
#使用制表符或换行符来添加空白 #
In programming, whitespace refers to any nonprinting character, such as spaces, tabs, newline characters.
1. Using tabs
>>> print (' Python ')
Python
>>> print (' \tpython ')
Python
2. Use newline characters
>>> print (' Python\c##\nphp\njavascript ')
python\c##
Php
Javascript
3. Tabs and newline characters used together
>>> print (' languages:\n\tpython\n\tc##\n\tphp ')
Languages:
Python
c##
Php
#删除多余的空白符 #
>>> favorite_language= ' python '
>>> Favorite_language
' Python '
You can see that there is a space in this variable, which is recognizable to the program, superfluous to a person, and how to remove
>>> Favorite_language.rstrip ()
' Python '
This is only temporary, in order to permanently delete the blank, you need to store this string in a variable
>>> Favorite_language=favorite_language.rstrip ()
>>> Favorite_language
' Python '
Culling characters at the beginning of a string
>>> name= ' Alben '
>>> Name
' Alben '
>>> Name.lstrip () #去头 #
' Alben '
>>> Name.rstrip () #去尾 #
' Alben '
>>> Name.strip () #去头去尾 #
' Alben '
P
Python character processing