name = “小明”
print(“大家好,我的名字叫%s” %name)
subjects = ["python","linux","mysql"]
sum = 0
for i in subjects:
print("请输入%s的成绩:"%i)
sore = int(input())
sum += sore
avg = sum / len(subjects)
print("平均成绩是",avg)
name = "xiaoming"
position = "讲师"
address = "北京市昌平区建材城西路金艳龙办公落1层"
print("----------------------------------------------")
print("姓名:%s"%name)
print("职位:%s"%position)
print("公司地址:%s"%address)
print("----------------------------------------------")
user_name = input("请输入用户名:")
print("用户名为:%s"%user_name)
user_password = input("请输入密码:")
print("密码为:%s"%user_password)
字符串中的每个字符都对应一个下标,下标编号是从0开始的.
切片的语法格式如下所示:
[起始:结束:步长]
假设有字符串 name = "abcdef"
name[0:3] ------> abc
name[3:5] ------> de
name[1:-1] ------> bcde
name[2:] ------> cdef
name[::-2] ------> fdb
name[3::-2] ------> db
str.find(str,beg=0,end=len(string))
# str -- 指定检索的字符串
# beg -- 开始索引,默认为0
# end -- 结束索引,默认为字符串的长度
string_example = 'hello world itheima and itheima and itheimaApp'
index = string_example.find("itheima")
print(index)
# 字符串里面有itheima 输出字符串位置12
string_example = 'hello world itheima and itheima and itheimaApp'
index = string_example.find("itcast")
print(index)
# 字符串里没有itacast 输出-1
str.index(str,beg=0,end=len(string))
# str -- 指定检索的字符串
# beg -- 开始索引,默认为0
# end -- 结束索引,默认为字符串的长度
# 如果找不到会给出一个异常
string_example = 'hello world itheima and itheima and itheimaApp'
index = string_example.index("itheima",0,10)
print(index)
# 异常:ValueError substring not found
str.count(sub,start=0,end=len(string))
# start -- 字符串开始搜索的位置
# end -- 字符串中结束搜索的位置
str.replace(ole,new[,max])
# old -- 将被替换的字符串
# new -- 新字符串,用于替换old字符串
# max -- 可选字符串,替换不超过max次
old_string = 'hello world itheima and itheima and itheimaApp'
new_string = old_string.replace("itheima","ITHEIMA",1)
print(new_string)
# 输出结果:hello world ITHEIMA and itheimaApp
str.split(str=“”,num=string.count(str))
# str -- 分隔符.默认为所有空字符.
# num -- 分割次
string_example = "this is string example....wow!!!"
print(string_example.split())
print(string_example.split('i',1))
print(string_example.split('w'))
print(string_example.split('i',5))
#输出结果
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']
['th', 's ', 's str', 'ng example....wow!!!']
str.capitalize()
str.title
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.title()
print(new_string)
#输出结果
Hello World Itheima And Itheimaapp
str.startswith(prefix[,start[,end]])
# prefix -- 检测的字符串.
# strbeg -- 可选参数用于设置字符串检测的起始位置.
# strend -- 可选参数用于设置字符串检测的结束位置.
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.startswith('hello')
print(new_string)
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.startswith('jiade')
print(new_string)
#输出结果
True
Flase
str.endswith(suffix[,start[,end]])
# suffix -- 该参数可以是一个字符串或者是一个元素.
# start -- 可选参数用于设置字符串检测的起始位置.
# end -- 可选参数用于设置字符串检测的结束位置.
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.endswith('itheimaApp')
print(new_string)
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.endswith('hello')
print(new_string)
#输出结果
True
Flase
str.upper()
mystr = ‘hello world itheima and itheimaApp’
newStr = mystr.upper()
print(newStr)
#输出结果
HELLO WORLD ITHEIMA AND ITHEIMAAPP
str.ljust(width[,fillchar])
# width -- 指定字符串长度
# fillchar -- 填充字符,默认为空格.
string_exmaple = 'Runoob example...wow!!!'
print(string_exmaple.ljust(50,'*'))
#输出结果
Runoob example...wow!!!***************************
str.rjust(width[,fillchar])
# width -- 指定字符串长度
# fillchar -- 填充字符,默认为空格.
string_exmaple = 'Runoob example...wow!!!'
print(string_exmaple.rjust(50,'*'))
#输出结果
***************************Runoob example...wow!!!
str.center(width[,fillchar])
# width -- 字符串的总宽度
# fillchar -- 填充字符
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.center(50)
print(new_string)
#输出结果
hello world itheima and itheimaApp
str.lstrip([chars])
# chars -- 指定删除的字符
old_string = ' hello world itheima and itheimaApp '
new_string = old_string.strip()
print(new_string)
#输出结果
hello world itheima and itheimaApp
str.rstrip([chars])
# chars -- 指定删除的字符
old_string = ' hello world itheima and itheimaApp '
new_string = old_string.strip()
print(new_string)
#输出结果
hello world itheima and itheimaApp
str.strip([chars])
# chars -- 指定删除首尾的字符
old_string = ' hello world itheima and itheimaApp '
new_string = old_string.strip()
print(new_string)
#输出结果
hello world itheima and itheimaApp
运算符 描述
+ 字符串连接
* 重复输出字符串
in 成员运算符.如果字符串中包含给定的字符,返回为True
not in 成员运算符.如果字符串中不包含给定的字符,返回为True
r/R 原始字符串:原样输出