1. String Index
Returns a string that can be any character. If the index is negative, it is equivalent to the number from the back to the front.
[Python]
>>> Str = "HelloWorld! "
>>> Print str [0]
H
>>> Print str [-4]
R
>>> Str = "HelloWorld! "
>>> Print str [0]
H
>>> Print str [-4]
R
2. String sharding
Fragment is to extract part of the content from a given string.
[Python]
>>> Str = "HelloWorld! "
>>> Print str [0]
H
>>> Print str [-4]
R
>>> Print str [1: 4]
Ell
>>> Print str [:-7]
Hell
>>> Print str [5:]
World!
>>> Str = "HelloWorld! "
>>> Print str [0]
H
>>> Print str [-4]
R
>>> Print str [1: 4]
Ell
>>> Print str [:-7]
Hell
>>> Print str [5:]
World!
Shard extension form:
Str [I, J, K] means to index every K elements from I to J-1. If K is negative, it is indexed from left to left.
[Python]
>>> Print str []
Loo
>>> Print str []
LloWo
>>> Print str []
Loo
>>> Print str []
LloWo
The ord function converts a character to the corresponding ASCII value, while the chr function converts a number to a character. For example:
[Python]
>>> Print ord ('A ')
97
>>> Print chr (97)
A
>>>
>>> Print ord ('A ')
97
>>> Print chr (97)
A
>>>
In Python, you can only change the value of a string.
Each time a string is modified, a New String object is generated, which seems to cause a reduction in efficiency. In fact, Python automatically recycles strings that are no longer used.
The new object reuses the space of the previous string.
String formatting:
[Python]
>>> "% D % s % d you! "% (1," goujinping ", 8)
'1 goujinping 8 you! '
>>> "% D % s % d you! "% (1," goujinping ", 8)
'1 goujinping 8 you! '