A string is an ordered collection of Python, and the sequence contains a left-to-right order-the elements in the sequence are stored and read according to their relative positions.
Operation of the string:
>>>s = ' Spam ' >>>s[0] #读取第0个字符 ' s ' >>>len (s) #字符串的长度4 >>>s[4] reads the 4th character, the more Boundary Access Error indexerror:string index out of Range>>>s[1:3] ' PA '
Reverse Access:
>>>s[-1] ' m ' >>>s[:-1] ' Spa ' >>>s[:] ' Spam ' >>>print s[-3:-1] ' pa '
As a sequence, strings also support the use of home-good merging:
>>>s = ' Spam ' >>>print (s+ ' xyz ') ' spamxyz ' >>>s*4 ' spamspamspamspam ' >>>print [S]*4 [' Spam ', ' Spam ', ' Spam ', ' Spam ']
Non-denaturing:
>>>s[0] = ' S ' TypeError: ' str ' object does not support item assignment>>>s = ' Z ' + s[1:] ' zpam '
Specific methods for strings:
#查找字符串出现首次的位置, if not found, return -1>>>s.find (' pa ') 1>>>s = ' Spampa ' #replace全局搜索并替代, return the new string >>>print S.replace (' pa ', ' xyz ') ' sxyzmxyz ' #replace不会修改原始的字符串 >>>s ' Spampa ' #分隔符将字符串拆分成子串 >>>line = ' Aaa,bbb,c Cc,ddd ' >>>line.split (', ') [' AAA ', ' BBB ', ' CCC ', ' DDD '] #大小写变换 >>>line.upper () ' AAA,BBB,CCC,DDD ' # Determine if all letters >>>line.isalpha () false# Remove the characters at the end of the line >>>line = ' aaa,bb b,ccc, ddd\n ' >>>line.rstrip () ' AAA,BB B,CCC, ddd ' >>>line = ' 8888aaa,bbbb,888888 ' >>>line.rstrip (' 8 ') 8888aaa,bbbb,
Formatting advanced substitution operations for strings, which can be used in the form of an expression and as a string method invocation:
>>>print ('%s,eggs,and%s%d '% (' spam ', ' spam ', ten) ') Spam,eggs,and spam>>>print (' {0}, eggs, and {1} '. Format (' spam ', ' spam! ')) Spam, eggs, and spam!
Ask for help Dir () and assist ()
>>>dir (S) [' __add__ ', ' __class__ ', ' __contains__ ', ', __delattr__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' ' __getattribute__ ', ' __getitem__ ', ' __getnewargs__ ' , ' __getslice__ ', ' __gt__ ', ' __hash__ ", ' __init__ ', ' __le__ ', ' __len__ ', ' _ _lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ',, ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ' , ' __repr__ ', ' __rmod__ ', ' __rmul__ ',, ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' _formatter_field_name_split ', ' _formatter_parser ', ' capitalize ', ' Center ', ' count ', ' decode ', ' encode ', ' endswith ', ' expandtabs ', ' find ', ' format ', ' index ', ' isalnum ', ' isalpha ', ' isdigit ', ' islower ', ' isspace ', ' istitle ', ' isupper ', ' join ', ' ljust ', ' lower ', ' ' Lstrip ', ' partition ', ' replace ', ' rFind ', ' rindex ', ' rjust ', ' rpartition ', ', ' rsplit ', ' Rstrip ', ' Split ', ' Splitlines ', ' startswith ', ' strip ', ', swapcase ', ' title ', ' Translate ', ' upper ', ' Zfill ']
The Dir function simply gives the name of the method. To query the specific usage of a specific method, you can pass the method to help ():
>>>help (s.replace) replace (...) S.replace (old, new[, Count])-string Return a copy of string S with all occurrences of substring old replaced B Y new. If The optional argument count is given, only the first count occurrences is replaced.
Other ways to write strings:
Three quotation marks (single or double quotation marks): Merges all the rows together and adds a newline character at the end of each line.
>>>msg = "" "AAAAAAAAAAABBB" "5555555555" "88888888 ' 222299999999999999" "" >>>MSGAAAAAAAAAAABBB " 5555555555 "" 88888888 ' 222299999999999999
Python also supports the original string constant (raw), which is the escape mechanism for removing backslashes:
>>>msg = "Abcd\nhhhhhh" abcdhhhhhh>>>msg = r "Abcd\nhhhhhh" Abcd\nhhhhhh
Python also supports Unicode string forms to support internationalization.
Pattern matching:
You need to import the RE module. This module contains calls like search, split, and replace.
>>>import Re>>>match = Re.match (' hello[\t]* (. *) world ', ' Hello Python world ') >>> Match.group (1) ' Python '
Python Learning: Strings (String)