掌握字符串的基本操作
连接两个字符串:+
重复字符串中的元素:*
测量字符串中元素的数量:len()
判断某个字符是否在字符串中:in
掌握字符串的方法(函数)的使用方法
查看方法:dir()
查看文档:help()
判断字符串是否是数字组成:isdigit()
分割和组合字符串:split()、join()
.lower()
.upper()
>>> a = '250'
>>> a.isdigit()
True
>>> '250hello'.isdigit()
False
>>> s = 'life is short you need python'
>>> lst = s.split(' ')
>>> lst
['life', 'is', 'short', 'you', 'need', 'python']
>>> '*'.join(lst)
'life*is*short*you*need*python'
>>> ' '.join(lst)
'life is short you need python'
字符串的格式化输出:
占位符
字符串的format()方法
内置函数format()
>>> 'I like {0}'.format('python')
'I like python'
>>> 'I like {0} and {1}'.format('python','physics')
'I like python and physics'
>>> 'I like {0:10} and {1:>15}'.format('python','physics')
'I like python and physics'
>>> 'I like {0:10} and {1:>15}'.format('python','physics')
'I like python and physics'
>>> 'I like {0:^10} and {1:>15}'.format('python','physics')
'I like python and physics'
>>> 'She is {0:4d} years old and {1:1f} in height.'.format(28,1.68)
'She is 28 years old and 1.680000 in height.'
>>> 'She is {0:4d} years old and {1:.1f} in height.'.format(28,1.68)
'She is 28 years old and 1.7 in height.'
>>> 'She is {0:04d} years old and {1:06.1f} in height.'.format(28,1.68)
'She is 0028 years old and 0001.7 in height.'
>>> h = 'hello'
>>> format(h,'->20')
'---------------hello'