python进阶(1)字符串
一、字符串的定义
一串字符,如"hello,world"
,'hello,python'
二、创建字符串
s=str()
print(s) #''
s=""
print(s) #''
s="hi"
print(s) #'hi'
三、字符串运算
加法
s1="hello,"
s2="python!"
s3=s1+s2
print(s3) #'hello,python!'
乘法
s1="hello"
s2=s1*3
print(s3) #'hellohellohello'
四、字符串切片
字符串[开始位置,结束位置(不包括),步长(可不写)]
例子
s="asdfghjkl"
print(s[0,5]) #'asdfg'
print(s[0,5,2]) #'adg'
五、字符串遍历
for i in 字符串:要做的事
例子
for i in "abcd":print(i)print("下一个")
结果
a
下一个
b
下一个
c
下一个
d
下一个