欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PythonDay6

程序员文章站 2022-03-16 08:55:25
...

每日一记

字符串的相关操作

'''
#字符串截取
s = "hello"
print(s[0:3])
print(s[:])
print(s[::-1])

#去空格
s = " hello "
#去左右的空格
print(s.strip())
print(s.lstrip())
print(s.rstrip())

#字符串赋值
s = "hello"
s_copy = s
print(s_copy)

#字符串拼接
s = "hello"
s1 = "python"
s2 = s + s1
print(s2)

# import operator   拼接
import operator
s = "hello"
s1 = "python"
s2 = operator.concat(s,s1)
print(s2)
b = operator.lt(s,s1)
print(b)

#求字符串长度
s = "hello"
s1 = "python"
print(len(s),len(s1))
s = "hello"
s1 = "python"
print(max(s),min(s1))
#字符串大小写转换
#upper             转换为大写
#lower              转为小写
#title              转换为标题
#capitalize             首字母大写
#swapcase               大写变小写,小写变大写
s = "asfDFGf   how  are  you"
print(s.upper())
print(s.lower())
print(s.title())
print(s.capitalize())
print(s.swapcase())
#字符串分割
s = "asfDFGf how  are  you"
ss = s.split("o")
print(ss)
#字符串序列连接
s = "asfDFGf how  are  you"
s1 =  "ghjkl"
s2 = s.join(s1)
print(s2)

a = ["hello","world"]
str = "-"
print(str.join(a))

#字符串内查找
s1 = 'today is a fine day'
index = s1.find("is",3,5)
print(index)
#查不到就返回-1  查到了就返回查到的首次的下标

#字符串内替换
s1 = 'today is a fine day is is is is is '
s = s1.replace("is","are",2)
print(s,s1)

#字符串判断
a = "asd"
a.isdigit() #是否由数字构成
a.isalnum() #是否由数字或字母构成
a.isalpha() #是否由字母构成
a.islower() #是否是大写   
a.isupper() #是否是小写
a.isspace() #是否由空格构成    
a.istitle() #是否是标题
相关标签: python 程序