all standard sequence operations (index, slice, multiplication, member check, length, minimum, maximum) apply to a string, but the string is immutable, so all element assignments and tile assignments are illegal.
A = ' http://www.python.org '
a[-3:]= ' com '
Traceback (most recent):
File "<pyshell#6>", line 1, in <module>
a[-3:]= ' com '
TypeError: ' str ' object does not support item assignment
Python provides a variety of string-setting methods, previously the primary workaround is to use string formatting operators-percent semicolons. This operator behaves like the attraction function in C: printf, which sets a string to the left of the% and specifies the value to be formatted on the right. Specifies the value to format, which can be used with a single value, using a tuple or dictionary.
Format= ' http//%s.%s.org '
values= (' www ', ' python ')
Format% values
'/HTTP www.python.org '
The above%s, called the conversion specifier, indicates where to insert the value, s means to format the value as a string. If the specified is not a string, it will be converted to a string using str, and other specifiers will result in other forms of conversion.
Another way to use it
"{},{},and {}". Format ("First", "second", "third")
' First,second,and third '
"{1},{0},{3},{2}". Format ("First", "second", "third", "four")
' Second,first,four,third '
"{0},{1},{2},{3}". Format ("First", "second", "third", "four")
' First,second,third,four '
# Call the Pi module in math
From Math import Pi
"{name} is approximately {value:.3f}.". Format (value=pi,name= "π")
' Πis approximately 3.142. '
Here value:.3f the format specifier, which means using a floating-point number format of 3 decimal places.
If the variable has the same name as the replacement field, you can use
From Math import E
F "Euler ' s constant is roughly {e}."
"Euler's constant is roughly 2.718281828459045."
YoY
"Euler ' s constant is roughly {e}.". Format (e=e)
"Euler's constant is roughly 2.718281828459045."
formatting strings
Component: Field name, conversion flag, format specifier.
Field Name: Index or identifier, indicating the format of the value to be set and replacing the field with the result. In addition to the specified value, you can specify a specific part of the value, such as an element.
Conversion flag: A single character following the exclamation mark, the currently supported characters include R (repr), S (str), a (ASCII). If a conversion flag is developed, the formatting mechanism of the object itself will not be applied, instead the object is converted to a string using the specified function, and further formatting is done.
Format specifier: The expression following the colon, the format specifier allows us to specify the final format in detail, including the format type (such as String, floating point, or hexadecimal).
Replace field name
"{} {} {} {}". Format (1,2,3,4)
' 1 2 3 4 '
#通过索引来指定那个字段中对应的未命名参数.
"{0} {3} {1} {2}". Format (1,2,3,4)
' 1 4 2 3 '
Basic conversions
Print ("{pi!s} {pi!r} {pi!a}". Format (pi= "π")
π ' π ' \u03c0 '
Convert using str, REPR, ASCII, respectively
You can also specify the type of the converted Value
"The number is {}". Format (42)
' The number is 42 '
"The number is {: F}". Format (42)
' The number is 42.000000 '
or in binary format
"The number is {: b}". Format (42)
' The number is 101010 '
string formatting type specifier
Integral type:
-
b binary
-
c character, convert the number to a character representing Unicode
-
d decimal
-
o octal
-
x 16 binary, showing lowercase letters
-
x 16 binary, showing uppercase letters
-
n behaves the same as D, using local numeric representations
-
floating point
e scientific notation, lowercase E
E scientific notation, capital E
F Display as fixed number, default six digits after decimal point
F with F
G automatic selection is indicated by scientific notation
G with G
N with G, using local representations
% use percent expressed
"(empty) with G
width, precision, and thousand characters
' {number:10} '. Format (number=3)
' 3 '
"{name:10}". Format (name= "Andy")
' Andy
"Pi Day is {pi:10.3f}". Format (PI=PI)
' Pi ' is 3.142 '
# or thousand characters
"This is {:,}". Format (10**10)
' This is 10,000,000,000 '
symbols, alignment, and padding
' {: 010.2f} '. Format (PI)
' 0000003.14 '
Print (' {0:<10.2f}\n{0:>10.2f}\n{0:^10.2f} '. Format (PI))
3.14
3.14
3.14
# you can make a character as a fill character
' {: $^15} '. Format ("Hello")
' $ $ Hello $ '
Print (' {0:10.2f}\n{1:10.2f} '. Format (PI,-PI))
3.14
-3.14
Print (' {0:10.2f}\n{1:=10.2f} '. Format (PI,-PI))
3.14
-3.14
Print (' {0:-.2f}\n{1:-.2f} '. Format (PI,-PI))
3.14
-3.14
Print (' {0:+.2f}\n{1:+.2f} '. Format (PI,-PI))
+3.14
-3.14
Print (' {0:+10.2f}\n{1:+10.2f} '. Format (PI,-PI))
+3.14
-3.14
Print (' {0:. 2f}\n{1:. 2f} '. Format (PI,-PI))
3.14
-3.14
' {: b} '. Format (42)
' 101010 '
' {: #b} '. Format (42)
' 0b101010 '
' {: #g} '. Format (42)
' 42.0000 '
String Common methods
Center centers strings by adding padding characters (by default, spaces) on both sides
"Hi,how old is You". Center (30)
' Hi,how old is you '
"Hi,how old is You". Center (30, "*")
' ******hi,how old is you****** '
Find finds a substring in a string, returns the index of the first character of the substring if found, otherwise returns-1.
' Hi,how old is you '. Find ("old")
7
TMP = "Hello,welcome to You"
Tmp.find (' Hi ')
-1
You can also specify the start and end of the search
Tmp.find ("Hi", 0,10)
-1
Join for merging strings
s = [' 1 ', ' 2 ', ' 3 ', ' 4 ']
S1= "+"
S1.join (s)
' 1+2+3+4 '
dir = ', ' usr ', ' bin ', ' env '
'/'. Join (dir)
'/usr/bin/env '
Print (' C: ' + ' \ \ '. Join (dir))
C:\usr\bin\env
Lower Convert lowercase
' HELLOWORLD '. Lower ()
' HelloWorld '
Replace replace replaces the specified string with another string and returns the replacement result.
str = "This is a test"
Str.replace ("is", "was")
' Thwas was a test '
Split splits a string into a sequence
num = ' 1+2+3+4 '
Num.split ("+")
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
'/usr/bin/env '. Split ('/')
[', ' usr ', ' bin ', ' env ']
Strip delete blank lines at the beginning and end of the line, and returns the result after the deletion
' Hello World '. Strip ()
' Hello World '
Getting started with Python: strings