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
comparing strings
#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)
Search from right: Str.rfind (' t ')
number of specified strings searched: Str.count (' t ')
all of the above methods can be replaced with index, the difference is to use the index to find not to 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 ()
Case Interchange: 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
get fixed length, right-aligned, There is not enough space on the left: str.ljust (width)
Gets fixed length, left alignment, right not enough space to be filled: str.ljust (width)
Get fixed length, middle alignment, not enough space on both sides: str.ljust (width)
Get fixed-length, right-aligned, left less than 0-padded
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, and there are some functions for replacing 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 by 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 '))
Splits a string by a 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-dependent
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 right-aligned, less part with 0 complement
Single quotation marks in a string, double quotation marks 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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python built-in string handling functions