First, character sequence
Python character variables differ from other languages because a special namespace makes the string non-directly changing
How do I specify a value for a specific position in the display string? As follows
>>> hellostring = "Hello World"
>>> Hellostring[0]
H
>>> Hellostring[5]
‘ ‘
>>> Hellostring[-1]
' d '
>>> Hellostring[-5]
W
As the example shows, the values in square brackets [] represent a specific position from left to right 0,1,2 .... From right to left -1,-2,-3 ....
Second, index and Shard
Python uses a half-open interval that contains the starting value within the range and does not contain an end value
1. Common Shards and indexes
>>> hellostring = "Hello World"
>>hellostring[:] #默认值起始位置为0, the end position is the last position of the final element
' Hello World '
>>> Hellostring[2:5]
' Llo '
>>> Hellostring[2:-2]
' Llo Wor '
>>>hellostring[-1] #只有一个参数则用作起始位置, the end position is the default value
' d '
2. Extending shards
Shards can also add a third parameter, specifying the step size of the Shard, which is the number of characters separated from the current character and the next character, which defaults to 1
, when the step is negative, it is extracted forward from the end of the string. Examples are as follows:
>>> digits = "0123456789"
>>> Digits[::-1] #从尾部向前提取, use more in Palindrome
' 9876543210 '
>>> Digits[1::2] #得到奇数分片
' 13579 '
>>> Digits[0::2] #得到偶数分片
' 02468 '
>>> Digits[-2::-2]
' 86420 '
>>> Digits[-1::-2]
' 97531 '
3. Copying shards
>>> Nameone = "OpenEx"
>>> nametwo = nameone[::-1]
>>> ID (nameone), ID (nametwo)
(5647840, 5648064)
>>> Nameone,nametwo
(' OpenEx ', ' Xenepo ')
Three, string manipulation
1. The "+" operator can be used as a two-string concatenated "*" operator to be used as a string copy
>>> A = "Hello"
>>> B = "World"
>>> a+b
' HelloWorld '
>>> a*2+ ("+b")
' Hellohello World '
2.in operator
can be used to detect whether a string contains a string
>>> ' ab ' in ' xxxxxabcc '
True
3. String functions and methods
The use of functions is not much different from C, which is similar to C + + method concepts such as Hellostring.upper (). Find ("A", 1,8)
Four, formatted output of the string
>>>import Math
>>> print ("%-10s,%10s\n%-5d,%5d\n%-8.2f,%.4f"% ("abc", "abc", 123,123,MATH.PI,MATH.PI))
ABC, ABC
123, 123
3.14, 3.1416
#类似C语言的printf格式化输出,%[symbol [width] [. Precision]code
How far palindrome Test Example:
Case insensitive
Ignoring special symbols, comparing numbers and letters only
Import string
TestString = input ("Please input the string for test:")
TestString = Teststring.lower () #将字符串中字母变为小写
Junk = String.punctuation+string.whitespace #利用预定义字符串, creating a collection of special symbols
For char in junk:
if (char in teststring):
TestString = Teststring.replace (char, "") #字符串替换方法, replace char with ""
if (teststring = = Teststring[::-1]):
Print ("%s\n%s"% (Teststring,teststring[::-1]))
Else
Print ("IT ' s not a palindrome\none1:%s\none2:%s"% (Teststring,teststring[::-1]))
Python Learning Diary---string