字符串很简单
下面我给你们列举了例子。
看完之后相信你们就能掌握字符串的基本方法。
一、字符串的修饰:
1、修饰方法有:
# 字符串的修饰:
# center 让字符串在指定长度居中.可以指定填充内容,默认以空格填充
# ljust 让字符串在指定长度左齐,可以指定填充内容,默认以空格填充
# rjust 让字符串在指定长度右齐,可以指定填充内容,默认以空格填充
# zfill 将字符串填充到指定内容的长度,不足的地方用零从左开始补充
# format 按照顺序,将后面的参数传递给前面的大括号
# strip 默认去掉两边的空格,去除内容可指定
# rstrip 默认去除右边的空格,去除内容可指定
# lstrip 默认去除左边的空格,去除内容可指定
2、这是字符修饰的例题样式:
# xenter
str_test = " for "
print(str_test.center(10))
print(str_test.center(10,"x"))
# ljust rjust
print(str_test.ljust(10))
print(str_test.rjust(10))
#zfill
print(str_test.zfill(10))
# format
python = "{0}is{1}"
print(python.format('for','coll'))
#strip rstrip lstrip
print(str_test.strip())
print(str_test.rstrip())
print(str_test.lstrip())
二、字符串的分割:
1、分割的方法有:
# 字符串的分割:
# partition 把mystr以str分割成三部分,str前,str自身和str后
# rpartition 类似于partition()函数,不过是从右往左
# splitlines 按照行分隔离,返回一个包含各行为元素的列表,按照换行符分割
# 字符串的替换:
# replace 从左到右替换指定的元素,可以指定替换的个数,默认全部替换
2、这是字符分割的例题样式:
# partition:
str_srst = "hello word"
print(str_srst.partition("o"))
# rpartition;
print(str_srst.rpartition("o"))
# splitlines:
my_str = "my\njava\npython"
print(my_str.splitlines())
# replace
str_srst = "hello word"
print(str_srst.replace("h","g"))
三、字符串的判断:
1、判断方法有:
# 字符串的判断:
# isalnum 判断字符串是否完全由字母或者数字组成
# isalpha 判断字符串是否完全由字母组成
# isdigit 判断字符串是否完全由由数字组成
# isupper 判断字符串当中的字母是否完全是大写
# islower 判断字符串当中的字母是否完全是小写
# istitle 判断字符串是否满足title格式
# isspace 判断字符串是否完全由空格组成
# startwith 判断字符串的开头字符,也可以截取判断
# endswith 判断字符串的结尾字符,也可以截取判断
# split 判断字符串的分隔符切片
2、这是字符判断的例题样式:
# isalnum 判断字符串是否完全由字母或者数字组成
str_test = "123456asd"
str_test1 = "12345"
print(str_test.isalnum())
print(str_test1.isalnum())
# isalpha 判断字符串是否完全由字母组成
str_test2 = "qwerty"
str_test3 = "1234qwer"
print(str_test2.isalpha())
print(str_test3.isalpha())
# isdigit 判断字符串是否完全由由数字组成
print("123456".isdigit())
print("qwe1234".isdigit())
# isupper 判断字符串当中的字母是否完全是大写
print("QWERT".isupper())
print("QWERTqwe".isupper())
# islower 判断字符串当中的字母是否完全是小写
print("qwerty".islower())
print("WERTwert".islower())
# istitle 判断字符串是否满足title格式
print("Hell".istitle())
print("hell java".istitle())
# isspace 判断字符串是否完全由空格组成
print(" ".isspace())
print("'a ".isspace())
# # startwith 判断字符串的开头字符,也可以截取判断
print("for i in range".startswith("f"))
print("fro i in range"[3:].startswith("f"))
# endswith 判断字符串的结尾字符,也可以截取判断
print("for i in ramge".endswith("e"))
print("for i i range"[2:].endswith("e"))
# split 判断字符串的分隔符切片
print("for i in range".split())
print("foriinrange".split())
四、字符串的变形:
1、变形方法有:
# 字符串的变形:
# upper 将字符串当中所有的字符串都改为大写
# lower 将字符串当中所有的字符串都改为小写
# swapcase 将字符串当中中所有的字母大小写互换
# title 将字符串当中的首字母大写,单词以首字母划分
# capitalize 自有字符串的首字母大写
2、这是字符变形的例题样式:
# upper
print("Hello python".upper())
# lower
print("Hello python".lower())
# swapcase
print("Hello python".swapcase())
# title
print("Hello python".title())
# captialize
print("Hello python".capitalize())
# expandtabs
print("Hello\t python\t".expandtabs(10))
五、字符串的占位符:
1、字符占位方法:
# %s 字符串
# %d 数字站位
# %f 浮点数的站位
# %.2f 控制浮点型数字的占位符
2、这是字符占位的例题样式:
# %s 字符串
print("may name is %s" %("for"))
# %d 数字站位
print("I am %d yesa old"%(16))
# %f 浮点数站位
print("I am heigth is %f"%(1.60))
# %.2f 浮点数站位,保留两位小数
print("I am heigth is %.2f"%(1.60))