美文网首页
Python字符串str常用操作

Python字符串str常用操作

作者: 小淼不卖萌 | 来源:发表于2018-10-10 00:13 被阅读0次

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常用操作

      本文链接:https://www.haomeiwen.com/subject/gpejnftx.html