string
Python strings are categorized as immutable sequences, meaning that the characters contained in these strings exist in order from left to right, and they cannot be modified in place.
String constants
Single quote ' spam '
Double quote "spam"
Three quotes "... spam ..." "" ... spam ... ""
Escape character "S\tp\na\om"
Raw string: R "C:\new\test.spam"
single and double quote strings are the same
In a Python string, the single and double quote characters are interchangeable.
use escape sequences to represent special characters
\ \ backslash (\)
\ ' single quotation mark (')
\ "Double quotation mark (")
\a Bells
\b Backwards
\f Page Change
\ nthe line break
\ r return
\ t Horizontal tab
\v Vertical Tab
\N{ID} Unicode Database ID
\uhhhh Unicode 16 is a hexadecimal number
\uhhhh Unicode 32-bit 16 forbidden number
\XHH Hex
\ooo octal
-Null
\other not escaped
Raw string Suppression escape
For example: myfile = open (' C:\new\test.dat ', ' W ')
The \ t will be replaced by a tab, causing an error
This formally uses the raw string to solve the problem, if the letter R appears in front of the first quotation mark of the string, it will close the escape mechanism, the result is that Python will be the backslash as a constant to maintain, so in order to avoid
This file name error, remember to add R in front of the string
MyFile = open (R ' C\new\test.dat ', ' W ')
There is another way, because two backslashes are an escape sequence of backslashes, which can be as follows
MyFile = open (' C:\\new\\test.dat ', ' W ')
strings in real-world applications
Basic operation:
Python
>>>len ("abc")
3
>>> ' abc ' + ' def ' string merge
' ABCdef '
>>> ' ni! ' String repetition
' ni!ni!ni!ni! '
>>>myjob = "Hacker"
>>>for C in Myjob:
Print (c,end= ");
H a C K e R
>>> ' K ' in Myjob
True
>>> ' Z ' in Myjob
False
>>> ' spam ' in ' abcdspamdef '
True
indexing and sharding of strings
Index: s[i] Gets the element with a specific offset
---The offset of the first element is 0
---Negative offset index means to start counting from the last or right reverse
---s[0] gets the first element
---s[-2] gets the second-lowest element
Shard: s[i:j] Extracts the corresponding part as a sequence
---Upper boundary is not included
The boundary of the---shard defaults to 0 and the length of the sequence, if not given,
---s[1:3] gets the element from offset 1, knowing but not including an element with an offset of 3
---s[1:] Gets the element from the offset to 1 knowing the end
---s[:3] gets the element from offset 0 to know but does not include an offset of 3
---s[:-1] gets the element from offset 0 to know but not including the last element
---s[:] gets the element from offset 0 to the end
extended shards: Third limit step of value
S[i:j:k] from I to j every K-element take once
>>>s = "ABCDEFGHMM"
>>>s[1:10:2]
' Acegm '
>>>S[::2]
' Acegm '
You can also use negative numbers as steps
>>>s = "Hello"
>>>S[::-1]
' Olleh '
With a negative step, the meaning of the two boundaries is actually flipped
The flip order of the Shard S[5:1:-1] gets elements from 2 to 5
>>>s = "ABCDEFG"
>>>S[5:1:-1]
' Fdec '
String Conversion Tool
It is not possible to add numbers and strings in Python, even if the string looks like a number.
>>> ' 42 ' +1
Error
The Int function converts a string to a number,
The STR function can convert a number to a string
The float function converts a string to a number
String Code Conversions
Similarly, a single character can be converted to its corresponding ASCLL code by passing it to the built-in Ord function, which actually returns the binary of the character in memory corresponding to that character. The CHR function performs the opposite action, obtaining the ASCLL code to convert it to the corresponding character
>>> Ord (' s ')
115
>>>CHR (115)
' s '
Int (' 1101 ', 2)
13
Bin (13)
' 0b1101 '
Modifying a string
Merge
s = s+ ' spam '
>>>s
' spamspam! '
>>>s = s[:4]+ ' Burger ' +s[-1]
>>>s
' spamburger! '
Replace
>>>s = ' Splot '
>>>s = s.replace (' pl ', ' pamal ', 1) Here 1 means to replace only once
>>>s
' Spamalot '
Formatting
>>> ' is%d%s bird! ' % (1, ' dead ')
That's 1 dead bird
>>> ' is {0} {1} bird! '. Format (1, ' dead ')
That's 1 dead bird
Find
>>>s = "Abcedef"
>>>where = S.find (' CE ')
>>>where
3
List
>>>s = [' spam ']
>>>l = List (s)
>>>l
[' s ', ' P ', ' a ', ' m ']
Tell a list into a string
>>>s = ". Join (L)
>>>s
' Spam '
To spell multiple strings
>>> ' spam '. Join ([' eggs ', ' sausage ', ' ham ')
' Eggsspamsausagespamhamspam '
text parsing of strings
>>>line = ' AAA bbb CCC '
>>>cols = Line.split ()
>>>cols
[' AAA ', ' BBB ', ' CCC ']
>>>line = ' bob,hacker,40 '
>>>line = Line.split (', ')
[' Bob ', ' Hacker ', ' 40 ']
>>>line = ' Iamspamaspamnumber '
>>>line = line.split (' spam ')
>>>line
[' Iam ', ' a ', ' number ']
Python learning string (top)