您的位置:首页 > 编程语言 > Python开发

Python学习笔记(四)字符串

2017-11-08 18:46 288 查看

获取字符串

str="hello"
print str[0] #输出h,第一个字符从0开始计数。


字符串拼接

1.可通过加号“+”将字符串拼接

2.乘号“*”代表将字符串重复输出

>>> str1="welcome"
>>> str2="girls"
>>> print str1+str2
welcomegirls
>>> print str*2
hellohello
>>>


格式化字符串(“%”)

格式化:将不同类型的变量通过%显示出来

>>> print "I am a %s and my height is %d cm!"%("girl",170)
I am a girl and my height is 170 cm!
#字符串用%s,整数用%d
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python