[Python基础]字符串操作

字符串

字符串的输出

下面是字符串的格式化输出

name = “小明”

print(“大家好,我的名字叫%s” %name)

利用%s输出元组里面的内容

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 

字符串的内建函数

find函数:检测字符串是否包含子字符串

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 

index函数:检测字符串是否包含子字符串

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 

count函数:统计字符串中某个字符的个数

str.count(sub,start=0,end=len(string))

# start -- 字符串开始搜索的位置
# end -- 字符串中结束搜索的位置

replace函数:将旧字符串替换为新字符串

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

splite函数:通过指定分隔符对字符串进行切片

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!!!']

capitalize函数:第一个字符大写,其他字符小写

str.capitalize()

title函数:所有单词首字母大写,其余字母小写

str.title
old_string = 'hello world itheima and itheimaApp'
new_string = old_string.title()
print(new_string)

#输出结果
Hello World Itheima And Itheimaapp

startswith函数:用于检测字符串是否是以指定子字符串,如果是,返回True,否则返回Flase.

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

endswith函数:检查字符串是否以制定子串结尾

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

upper函数:将小写字母转化为大写字母

str.upper()
mystr = ‘hello world itheima and itheimaApp’
newStr = mystr.upper()
print(newStr)

#输出结果
HELLO WORLD ITHEIMA AND ITHEIMAAPP

ljust函数:左对齐,使用空格填充至指定长度的新字符串

str.ljust(width[,fillchar])

# width -- 指定字符串长度
# fillchar -- 填充字符,默认为空格.
string_exmaple = 'Runoob example...wow!!!'
print(string_exmaple.ljust(50,'*'))

#输出结果
Runoob example...wow!!!***************************

rjust函数:右对齐,使用空格填充至指定长度的新字符串

str.rjust(width[,fillchar])

# width -- 指定字符串长度
# fillchar -- 填充字符,默认为空格.
string_exmaple = 'Runoob example...wow!!!'
print(string_exmaple.rjust(50,'*'))

#输出结果
***************************Runoob example...wow!!!

center函数:返回一个指定的宽度width居中的字符串

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        

lstrip函数:截掉字符串左边的空格或指定字符

str.lstrip([chars])

# chars -- 指定删除的字符
old_string = '    hello world itheima and itheimaApp   '
new_string = old_string.strip()
print(new_string)

#输出结果
hello world itheima and itheimaApp

rstrip函数:截掉字符串右边的空格或指定字符

str.rstrip([chars])

# chars -- 指定删除的字符
old_string = '    hello world itheima and itheimaApp   '
new_string = old_string.strip()
print(new_string)

#输出结果
hello world itheima and itheimaApp

strip函数:截掉字符串首尾的空格或指定字符

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          原始字符串:原样输出

你可能感兴趣的:(python,字符串)