Replace String
replace function
1
2a = 'hello word'
b = a.replace('word','python')regex
1
2
3
4import re
a = 'hello word'
pattern = re.compile('word')
b = pattern.sub('python',a)
Change line inside
- three single qute ‘’’
- three double qute “””
- one backslash \
Thread sleep
1 | import time |
Real time print
1 | import time |
Basic function
type transfer
int()
1
2int('7')
int('17', 16)str()
1
2str(7)
hex(17)
string
remove blank / special characters
s.strip().lstrip().rstrip(',')
copy
1
2
3
4
5#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2concat
1
2
3
4
5#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1find
1
2
3
4
5
6#strchr(sStr1,sStr2)
# < 0 not found
sStr1 = 'strchr'
sStr2 = 's'
nPos = sStr1.index(sStr2)
print nPoscompare
1
2
3
4#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)scan 4 contain
1
2
3
4
5#strspn(sStr1,sStr2)
sStr1 = '12345678'
sStr2 = '456'
#sStr1 and chars both in sStr1 and sStr2
print len(sStr1 and sStr2)length
1
2
3#strlen(sStr1)
sStr1 = 'strlen'
print len(sStr1)case
1
2
3
4
5#strlwr(sStr1)
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
#sStr1 = sStr1.lower()
print sStr1add pointed string
1
2
3
4
5
6#strncat(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = 'abcdef'
n = 3
sStr1 += sStr2[0:n]
print sStr1compare pointed string
1
2
3
4
5#strncmp(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = '123bc'
n = 3
print cmp(sStr1[0:n],sStr2[0:n])copy pointed string
1
2
3
4
5
6#strncpy(sStr1,sStr2,n)
sStr1 = ''
sStr2 = '12345'
n = 3
sStr1 = sStr2[0:n]
print sStr1replace pointed string
1
2
3
4
5
6#strnset(sStr1,ch,n)
sStr1 = '12345'
ch = 'r'
n = 3
sStr1 = n * ch + sStr1[3:]
print sStr1scan string
1
2
3
4
5
6
7
8
9#strpbrk(sStr1,sStr2)
sStr1 = 'cekjgdklab'
sStr2 = 'gka'
nPos = -1
for c in sStr1:
if c in sStr2:
nPos = sStr1.index(c)
break
print nPosreverse string
1
2
3
4#strrev(sStr1)
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]
print sStr1split string
1
2
3
4
5
6
7
8#strtok(sStr1,sStr2)
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print sStr1
#or
s = 'ab,cde,fgh,ijk'
print(s.split(','))connect string
1
2
3delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)