|
|
|
Create / delete
|
Create string
|
a=""
|
a="abcdef"
|
abcdef
|
Initialise
|
Fixed contents
|
a=""
|
a="abcdef";
|
abcdef
|
|
Repeated value 'x' x 10
|
c='x'
|
a=c*10
|
xxxxxxxxxx
|
Count
|
Length of string
|
a="abc"
|
i=len(a)
|
3
|
|
Number of 'X' chars in string
|
a="abcXdefXX"
|
n=a.count('X')
|
3
|
|
Number of 'X', 'Y' and 'Z' chars in string
|
a="abcXdefYZ"
|
n=a .count('X') +a .count('Y')+a.count('Z')
|
3
|
|
Number of 'abc' substrings in string
|
a="abcabcdefabc"
|
n=a.count('abc')
|
3
|
Retrieve by index
|
Retrieve 1st char
|
a="abc";
|
c=a[0]
|
a
|
|
Retrieve last char
|
a="abc";
|
c=a[-1]
|
c
|
|
Retrieve char 'n'
|
a="abcdef"; n=2;
|
c=a[n]
|
c
|
|
Retrieve out-of-bounds char
|
a="abc"; n=4
|
try: c=a[n] except: c="out of range"
|
out of range
|
Slice
|
Slice from index 'm' to 'n' (inclusive)
|
a="abcdefghi"; m=3; n=6;
|
s=a[m:n+1]
|
defg
|
|
Slice from index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
s=a[n:]
|
defghi
|
|
Slice up to index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
s=a[:n+1]
|
abcd
|
Compare
|
Test for equality (case sensitive)
|
a="abcdef"; b="abc";
|
t=(a==b)
|
0
|
|
Test for equality (case insensitive)
|
a="abcdef"; b="ABCdef";
|
t=(re.match("^"+a+"$", b, re.IGNORECASE)!=None)
|
1
|
|
Test for prefix
|
a="abcdef"; b="abc";
|
t=(re .match("^" +b+".*",a)!=None)
|
1
|
|
Test for suffix
|
a="abcdef"; b="def";
|
t=(re .match(".*" +b+"$",a)!=None)
|
1
|
Search
|
Test char 'is in' string
|
a="abcdefghi"; c='c';
|
t=(string.find(a,c) != -1)
|
1
|
|
Test substring 'is in' string
|
a="abcdefghi"; b="abc";
|
t=(string.find(a,b) != -1)
|
1
|
|
Find index of char (forward)
|
a="abcdefccccc"; c='c';
|
i=string.index(a,c)
|
2
|
|
Find index of char (reverse)
|
a="abcdefccccc"; c='c';
|
i=string.rindex(a,c)
|
10
|
|
Find index of substring (forward)
|
a="abcdefabc"; b="abc";
|
i=string.index(a,b)
|
0
|
|
Find index of substring (reverse)
|
a="abcdefabc"; b="abc";
|
i=string.rindex(a,b)
|
6
|
Split
|
Split on char
|
a="one,two,three"; c=',';
|
sa=string.split(a,c)
|
one two three
|
|
Split on string
|
a="aa_x_bb_x_cc"; b="_x_";a
|
sa=string.split(a,b)
|
aa bb cc
|
|
Split limited to 'n' parts
|
a="one,two,three"; c=','; n=2;
|
sa=string.split(a,c,n-1)
|
one two,three
|
Iterate
|
Iterate through string
|
a="abcdef"; b=""
|
for x in a: b=(b+x+',')
|
a,b,c,d,e,f,
|
Evaluate
|
Evaluate empty string
|
a="" t=0
|
if a: t=1
|
0
|
|
Evaluate non-empty string
|
a="abcdef" t=0
|
if a: t=1
|
1
|
Print
|
Formatting sprintf style
|
a="abc"; b="abcdef"
|
s="%10s %10s" % ( a, b )
|
abc abcdef
|
Insert
|
Prepend char
|
a="bcdef"; c='a';
|
a=c+a
|
abcdef
|
|
Append char
|
a="abc"; c='d';
|
a=a+c
|
abcd
|
|
Insert char at index 'n'
|
a="abc"; c='X'; n=1;
|
a=a[:n]+c+a[n:]
|
aXbc
|
|
Prepend string
|
a="def" b="abc"
|
a=b+a
|
abcdef
|
|
Append string
|
a="abc" b="def"
|
a=a+b
|
abcdef
|
|
Insert string at index 'n'
|
a="abcghi"; b="def"; n=3;
|
a=a[:n]+b+a[n:]
|
abcdefghi
|
|
Concatenate 'a' and 'b'
|
a="abc"; b="def";
|
s=a+b
|
abcdef
|
Overwrite
|
Overwrite char at index 'n'
|
a="abc"; n=1; c='B';
|
ca=array('c'); ca.fromstring(a); ca[n]='B'; a=ca.tostring()
|
aBc
|
|
Overwrite substring at index 'n'
|
a="abcdefghi" b="DEF" n=3
|
a=a[:n]+b+a[n+len(b):]
|
abcDEFghi
|
Replace
|
Replace 1st instance of char 'X' with 'Y'
|
a="XXXYYYZZZ"
|
a=a.replace('X','Y',1)
|
YXXYYYZZZ
|
|
Replace all instances of char 'X' with 'Y'
|
a="XXXYYYZZZ";
|
a=a.replace('X','Y')
|
YYYYYYZZZ
|
|
Replace 1st instance of substring 'abc' with 'XYZ'
|
a="abcdefabc"
|
a=a .replace("abc","XYZ",1)
|
XYZdefabc
|
|
Replace all instances of substring 'abc' with 'XYZ'
|
a="abcdefabc"
|
a=a.replace("abc","XYZ")
|
XYZdefXYZ
|
|
Replace all instances of 'X','Y','Z' with 'A','B','C' respectively
|
a="XXXYYYZZZ";
|
a=a .replace('X' ,'A') .replace('Y' ,'B').replace('Z','C')
|
AAABBBCCC
|
Remove
|
Remove 1st instance of char 'X'
|
a="XXXYYYZZZ"
|
a=a.replace('X','',1)
|
XXYYYZZZ
|
|
Remove all instances of char 'X'
|
a="XXXYYYZZZ"
|
a=a.replace('X','')
|
YYYZZZ
|
|
Remove 1st instance of substring 'abc'
|
a="abcdefabc"
|
a=a.replace("abc","",1)
|
defabc
|
|
Remove all instances of substring 'abc'
|
a="abcdefabc"
|
a=a.replace("abc","")
|
def
|
|
Remove prefix 'abc.'
|
a="abc.abc.def"
|
a=re.sub(r"^abc.","",a)
|
abc.def
|
|
Remove suffix '.def'
|
a="abc.def.def"
|
a=re.sub(r"\.def$","",a)
|
abc.def
|
Remove by index
|
Remove 1st char
|
a="abcdef";
|
a=a[1:]
|
bcdef
|
|
Remove last char
|
a="abcdefghi";
|
a=a[:-1]
|
abcdefgh
|
|
Remove char at index 'n'
|
a="abcdef"; n=3;
|
a=a[:n]+a[n+1:]
|
abcef
|
|
Remove chars in index range 'm' to 'n' (inclusive)
|
a="abcdef"; m=2; n=4;
|
a=a[:m]+a[n+1:]
|
abf
|
|
Remove chars from index 'n' onwards (inclusive)
|
a="abcdefghi"; n=3;
|
a=a[:n]
|
abc
|
|
Remove chars up to index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
a=a[n+1:]
|
efghi
|
Trim
|
Trim leading white space
|
a=" abc def "
|
a=string.lstrip(a)
|
abc def
|
|
Trim trailing white space
|
a=" abc def "
|
a=string.rstrip(a)
|
abc def
|
|
Trim leading and trailing white space
|
a=" abc def "
|
a=string.strip(a)
|
abc def
|
|
Trim trailing carriage return
|
a="abcdef\n"
|
a=string.strip(a)
|
abcdef
|
Sort
|
Sort in ascending order
|
a="cafdeb";
|
am=map(None,a); am.sort(); a=string.join(am,"")
|
abcdef
|
|
Sort in descending order
|
a="cafdeb";
|
am=map(None,a); am.sort(); am.reverse(); a=string.join(am,"")
|
fedcba
|
|
Sort in custom order
|
a="cafdeb";
|
am=map(None,a); am.sort(comp); a=string.join(am,"")
|
fedcba
|
|
Reverse
|
a="abcdefghi";
|
aa=array('c',a); aa.reverse(); a=string.join(aa,"")
|
ihgfedcba
|
Convert case
|
Convert to lower case
|
a="ABCDEFGHI";
|
a=a.lower()
|
abcdefghi
|
|
Convert to upper case
|
a="abcdefghi";
|
a=a.upper()
|
ABCDEFGHI
|
|
Convert first char to lower case
|
a="ABCDEFGHI";
|
a=a[0].lower()+a[1:]
|
aBCDEFGHI
|
|
Convert first char to upper case
|
a="abcdefghi";
|
a=a.capitalize()
|
Abcdefghi
|
|