Python字符串str常用操作

1. 连接 join()

In [2]: a = '...'.join(['1', '2', '3'])

In [3]: a
Out[3]: '1...2...3'
  • a = '...'.join([1,2,3])会报错:
    TypeError: sequence item 0: expected string, int found
    应改为
    In [6]: a = '...'.join([str(x) for x in [1, 2, 3]])
    
    In [7]: a
    Out[7]: '1...2...3'
    

2. 分隔split()

In [9]: b =  '1...2...3'.split('...')

In [10]: b
Out[10]: ['1', '2', '3']


3. 小写str.lower()

4. 大写str.upper()

5. 可转化为数字str.isdigit()

6. 去尾 str.strip()

你可能感兴趣的:(Python字符串str常用操作)