'''
字符串的基础语法和操作
1、定义
2、访问,获取字符串中的元素
3、遍历字符串
4、基本的简单操作
5、转义字符
'''
1、定义
str ='hello world'
print(str)r)
2、访问,获取字符串中的元素
字符串的访问,是通过索引下标实现的
下标开始是0
str ='hello world'
print(str[1]) #正序
print(str[-1]) #反序
3、遍历字符串
#方式1 提取字符串中具体的值
for i in str:
print(i)
#方式2 通过循环提取字符串下标的值
for index in range(0,len(str)): #[0,1,2,3,4]
print(str[index])
4、基本的简单操作
4.1、加
str1='hello'
str2='world'
print(str1+' '+str2) #加
4.2、乘
# 打印**********你好**********
print('*'*10+'你好'+'*'*10) #乘法
4.3、in
str1='hollo'
if 'e' in str1:
print('%s字符串中包含e'%str1)
else:
print('%s字符串中不包含e'%str1)
4.4、not in
if 'e' not in str1:
print('%s字符串中不包含e'%str1)
else:
print('%s字符串中包含e'%str1)
4.5、r/R
print('hello\nworle') #\换行
print(r'hello\nworle')
5、转义字符
\n 换行
\t 制表符 相当于tab键
\r 回车 光标回到本行的首位,就相当于把前面的内容覆盖
print('helle"hello')
print("hello'hello")
print('hello\thello')
print('hello\rhaha')
网友评论