python basic 1

Replace String

  1. replace function

    1
    2
    a = 'hello word'
    b = a.replace('word','python')
  2. regex

    1
    2
    3
    4
    import re
    a = 'hello word'
    pattern = re.compile('word')
    b = pattern.sub('python',a)

Change line inside

  1. three single qute ‘’’
  2. three double qute “””
  3. one backslash \

Thread sleep

1
2
import time
time.sleep(7)

Real time print

1
2
3
4
5
import time
import sys
print 'hello word'
sys.stdout.flush()
time.sleep(7)

Basic function

type transfer

  • int()

    1
    2
    int('7')
    int('17', 16)
  • str()

    1
    2
    str(7)
    hex(17)

string

  1. remove blank / special characters
    s.strip().lstrip().rstrip(',')

  2. copy

    1
    2
    3
    4
    5
    #strcpy(sStr1,sStr2)
    sStr1 = 'strcpy'
    sStr2 = sStr1
    sStr1 = 'strcpy2'
    print sStr2
  3. concat

    1
    2
    3
    4
    5
    #strcat(sStr1,sStr2)
    sStr1 = 'strcat'
    sStr2 = 'append'
    sStr1 += sStr2
    print sStr1
  4. find

    1
    2
    3
    4
    5
    6
    #strchr(sStr1,sStr2)
    # < 0 not found
    sStr1 = 'strchr'
    sStr2 = 's'
    nPos = sStr1.index(sStr2)
    print nPos
  5. compare

    1
    2
    3
    4
    #strcmp(sStr1,sStr2)
    sStr1 = 'strchr'
    sStr2 = 'strch'
    print cmp(sStr1,sStr2)
  6. 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)
  7. length

    1
    2
    3
    #strlen(sStr1)
    sStr1 = 'strlen'
    print len(sStr1)
  8. case

    1
    2
    3
    4
    5
    #strlwr(sStr1)
    sStr1 = 'JCstrlwr'
    sStr1 = sStr1.upper()
    #sStr1 = sStr1.lower()
    print sStr1
  9. add pointed string

    1
    2
    3
    4
    5
    6
    #strncat(sStr1,sStr2,n)
    sStr1 = '12345'
    sStr2 = 'abcdef'
    n = 3
    sStr1 += sStr2[0:n]
    print sStr1
  10. compare 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])
  11. copy pointed string

    1
    2
    3
    4
    5
    6
    #strncpy(sStr1,sStr2,n)
    sStr1 = ''
    sStr2 = '12345'
    n = 3
    sStr1 = sStr2[0:n]
    print sStr1
  12. replace pointed string

    1
    2
    3
    4
    5
    6
    #strnset(sStr1,ch,n)
    sStr1 = '12345'
    ch = 'r'
    n = 3
    sStr1 = n * ch + sStr1[3:]
    print sStr1
  13. scan 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 nPos
  14. reverse string

    1
    2
    3
    4
    #strrev(sStr1)
    sStr1 = 'abcdefg'
    sStr1 = sStr1[::-1]
    print sStr1
  15. split 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(','))
  16. connect string

    1
    2
    3
    delimiter = ','
    mylist = ['Brazil', 'Russia', 'India', 'China']
    print delimiter.join(mylist)