Generating String variables
str= ' python String function '
string length gets : Len (str)
Example: print '%s length=%d '% (Str,len (str))
Connection string
sStr1 = ' strcat '
sStr2 = ' Append '
sStr1 + = SStr2
Print SStr1
copy string
#strcpy (SSTR1,SSTR2)
sStr1 = ' strcpy '
sStr2 = sStr1
sStr1 = ' strcpy2 '
Print SSTR2
comparison string
#strcmp (SSTR1,SSTR2)
sStr1 = ' STRCHR '
sStr2 = ' Strch '
Print cmp (SSTR1,SSTR2)
String Search related
search for specified string, no return -1:str.find (' t ')
Specify starting Position search: Str.find (' t ', start)
specify start and end position search: Str.find (' t ', start,end)
from the right to start looking for: Str.rfind (' t ')
number of specified strings searched: Str.count (' t ')
all of the above methods can be replaced by index. The difference is that using index lookup does not throw an exception, and find returns-1
print '%s find nono=%d '% (str,str.find (' Nono '))
print '%s find t=%d '% (str,str.find (' t '))
print '%s find t from%d=%d '% (str,1,str.find (' t ', 1))
print '%s find t from%d to%d=%d '% (str,1,2,str.find (' t ', $))
#print '%s index Nono '% (Str,str.index (' Nono ', up))
print '%s rfind t=%d '% (str,str.rfind (' t '))
print '%s Count t=%d '% (str,str.count (' t '))
letter handling
All caps: Str.upper ()
All lowercase: str.lower ()
Uppercase and lowercase swaps: str.swapcase ()
uppercase. Remaining lowercase: str.capitalize ()
First uppercase: Str.title ()
print '%s lower=%s '% (Str,str.lower ())
print '%s upper=%s '% (str, Str.upper ())
print '%s swapcase=%s '% (Str,str.swapcase ())
print '%s capitalize=%s '% (str,str.capitalize ())
print '%s title=%s '% (Str,str.title ())
formatting related
gets fixed length, right alignment, left insufficient space to complement: str.ljust (width) The
gets the fixed length. Left-aligned, the right is not enough space to complement: str.ljust (width)
To get fixed length, middle alignment, both sides not enough space to do: str.ljust (width)
Get fixed length, right alignment, left insufficient 0 to complement
print '%s ljust=%s '% (Str,str.ljust ())
print '%s rjust=%s '% (Str,str.rjust ())
print '%s center=%s '% (Str,str.center ( )
print '%s zfill=%s '% (Str,str.zfill ())
String substitution related
Replace old to New:str.replace (' old ', ' new ')
Replace the specified number of times with old as New:str.replace (' old ', ' new ', maxreplacetimes)
print '%s replace T to *=%s '% (str,str.replace (' t ', ' * '))
print '%s replace T to *=%s '% (str,str.replace (' t ', ' * ', 1))
S.replace (Oldstr, Newstr, [Count]) #把S中的oldstr替换为newstr, Count is the number of replacements. This is a common form of substitution. Other functions are substituted for special characters
S.strip ([chars]) #把S中前后chars中有的字符所有去掉, can be understood to replace s before and after chars to none
S.lstrip ([chars])
S.rstrip ([chars])
S.expandtabs ([tabsize]) #把S中的tab字符替换没空格. Each tab is replaced with a tabsize space, which defaults to 8
String go to space and go to specify character
Go on both sides Space: Str.strip ()
Left space: Str.lstrip ()
Go right Space: Str.rstrip ()
Go to both sides of the string: Str.strip (' d '), corresponding to the Lstrip,rstrip
str= ' python String function '
print '%s strip=%s '% (Str,str.strip ())
str= ' python String function '
print '%s strip=%s '% (Str,str.strip (' d '))
Cut string by specified character array: Str.split (")
By default, separated by spaces
Str= ' a b C de '
print '%s strip=%s '% (Str,str.split ())
Str= ' A-b-c-de '
print '%s strip=%s '% (str,str.split ('-'))
string Inference related
whether to start with start: Str.startswith (' Start ')
Whether to end With end: Str.endswith (' End ')
is all letters or numbers: Str.isalnum ()
Whether full-letter: Str.isalpha ()
is full number: Str.isdigit ()
All lowercase: str.islower ()
All uppercase: Str.isupper ()
str= ' python String function '
print '%s startwith t=%s '% (str, Str.startswith (' t '))
print '%s endwith d=%s '% (Str,str.endswith (' d '))
print '%s isalnum=%s '% (Str,str.isalnum ()
str= ' pythonstringfunction '
print '%s isalnum=%s '% (Str,str.isalnum ())
print '%s isalpha=%s '% (str, Str.isalpha ())
print '%s isupper=%s '% (Str,str.isupper ())
print '%s islower=%s '% (Str,str.islower ())
print ' %s isdigit=%s '% (Str,str.isdigit ())
str= ' 3423 '
print '%s isdigit=%s '% (Str,str.isdigit ())
The conversion of a Python string to a number into a string str ()
The string becomes a number string.atoi (S,[,base])//base is the binary cardinality
Floating-point conversion string.atof (s)
The alignment of the string at output :
S.ljust (Width,[fillchar]) #输出width个字符, s left-aligned, the insufficient portion is filled with Fillchar, the default is a space.
S.rjust (Width,[fillchar]) #右对齐
S.center (width, [Fillchar]) #中间对齐
S.zfill (width) #把S变成width长. and align to the right. The shortfall is supplemented by 0.
The single argument in the string. Double-quotes are escaped with \.
How to convert a string into a number?
int (' 1234 ')
In the string module there are
Import string
>>> a= "12345"
>>> Import String
>>> String.atoi (a)
12345
>>> b= "123.678"
>>> String.atof (b)
123.678
Python built-in string handling functions