字符串的创建
str1 = 'hello'
str2 = "world"
str3 = """ni hao"""
print(str3)
ni hao
字符串的查询
str1[0:6:2]
'hlo'
str1[-3:-1]
'll'
字符串的删除
del str1
str2.replace('w','W')
'World'
str2.lower()
'world'
字符的拼接
str3+' '+str2
'ni hao world'
'!'.join(str2)
'w!o!r!l!d'
str4 = ' '.join([str2,str3])
str4
'world ni hao'
str4.split(' ')
['world', 'ni', 'hao']
print("hello\n")
print("hello")
hello
hello
str5 = 'hello\n'
print(str5)
str5.strip()
hello
'hello'
f = open('test.txt','w')
f.write("hello world,\n")
f.write("I'm here!")
9
f.close()
f = open('test.txt','r')
lines = f.readlines()
lines
['hello world,\n', "I'm here!"]
for line in lines:
print(line.split(' '))
['hello', 'world,\n']
["I'm", 'here!']