Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> str = 'hello python'
>>> str
'hello python'
>>> str_s = 'hello' +'python'
>>> str_s
'hellopython'
>>> str*2
'hello pythonhello python'
>>> str
'hello python'
>>> len(str)
12
>>> #字符串操作
>>> str=
SyntaxError: invalid syntax
>>> str='1 2 3 4 5'
>>> str
'1 2 3 4 5'
>>> str.split() #切割
['1', '2', '3', '4', '5']
>>> str='1,2,3,4,5'
>>> str.split(',')
['1', '2', '3', '4', '5']
>>> str=str.split(',')
>>> str
['1', '2', '3', '4', '5']
>>> str.join() #合并
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
str.join()
AttributeError: 'list' object has no attribute 'join'
>>> str_s=''
>>> str_s.join(str)
'12345'
>>> str='python'
>>> str.replace('p','hello p') #替换
'hello python'
>>> str.upper() #大写
'PYTHON'
>>> str.lower()
'python'
>>> str=' hello python'
>>> str.strip() #去掉前后空格
'hello python'
>>> str
' hello python'
>>> str.lstrip()
'hello python'
>>> #同理,右边的空格一样
>>> '{} {} {}'.format('liu' ,'yong','hello')
'liu yong hello'
>>> '{2},{1}'.format('liu','yong')
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
'{2},{1}'.format('liu','yong')
IndexError: tuple index out of range
>>> '{1} {0}'.format('liu','yong')
'yong liu'
>>> str = 'liu yong'
>>> a=123
>>> b=456
>>> c=789
>>> result ='%s %d %d' %(str,a,b)
>>> result
'liu yong 123 456'
>>>