Python 字符串语法,for

#-*- coding:utf-8 -*-

#字符串的拼接方式一

a = "hello"

b = "world"

c = a + " " + b

#打印 hello world
print(c)

d = "lao"

e = "wang"

#字符串输出
print("%s"%(d+e)) #打印 laowang

#字符串拼接方式二
f = "==%s=%s="%(c,d+e)
print("******%s*******"%(f))


'''
for 临时变量 in 列表或者字符串等:
[tab键] 循环满足条件时执行的代码
else:
[tab键] 循环不满足条件时执行的代码


else后面的语句一定会执行,因为总有条件不满足的时候

'''

#字符串的遍历

for index in c:
        print("%s"%index,end='')
'''
else:
        print("no world .")
'''

print("")

#字符串的下标,下标从0开始,0表示第一个字符
print(c[0])
print(c[1])

#len()函数,用来计算字符串中字符的个数(不包括'/0')
print(c[len(c)-1])

print("==============")
#字符串逆序,逆序下标从-1开始,-1表示最后一个字符
print(c[-1])

猜你喜欢

转载自www.cnblogs.com/zhanggaofeng/p/9222526.html