#1.swapcase():大小写互换
text = "GIS is cool"
print (text.swapcase())
#gis IS COOL
#2.capitalize():首字母大写,其余小写
text = "gis is cool"
print (text.capitalize())
#Gis is cool
#3.lower:大写字母转小写
text = "gis is cool"
print (text.lower())
#gis is cool
#4.uppeer:大写字母转小写
text = "GIS is cool"
print (text.upper())
#GIS IS COOL
#5.title:返回标题化字符串
text = "GIS is cool"
print (text.title())
#Gis Is Cool
#6.索引值[]
text = "GIS is cool"
print (text[0])
#返回第一个字符:G
print (text[-1])
#返回最后一个字符:l
#7.切片操作符‘:’
text = "GIS is cool"
print (text[4:])
#第4个字符到最后:is cool
print (text[:4])
#开头到第3个字符:GIS
print (text[1:3])
#第1个到第2个字符:IS
#8.find():确定一个子串是否包含在原字符串,包含则返回第一个符合字符的索引值,否则返回-1.
text = "GIS is cool"
print(text.find('IS')) #1
print(text.find('Is')) #-1
#9.in操作符类似于find方法,但是返回的是布尔值。
text = "GIS is cool"
print('IS' in text) #true
print(text.find('Is' in text) #false
#10.join():将字符串列表中的所有元素合并为一个字符串
list_str=["hello", "world", "!"]
str8 = ' '
str8.join(list_str)
# 'hello world !'
#11.spilt():使用指定分隔符分割原字符串为字符串列表
point = '123,123,987 '
point.split(',')
# ['123', '123', '987 ']
#12.strip():用于删除原字符串中起始处和末尾处,属于字符串参数中的字符。如果参数为空,则删除字符串中的空格。
str0 = "apple is very delicious"
str0.strip("aus")
#'pple is very delicio'
str0.lstrip('ap')
#'le is very delicious'
str0.rstrip('suo')
#'apple is very delici'
str1 = " Hello World. "
str1.strip()
#'Hello World.'
#13.replace():将字符串中的一个子串替换成另外一个子串
word = "My name is Andy"
print (word.replace("Andy","Bob"))
# My name is Bob
str2 = "Hello World."
print (str2.replace("l", "L", 3))
# HeLLo WorLd.
name = "123.shp" #去文件名后缀
print (name.replace(".shp",""))
#123
#14.format():用于字符串格式化的方法
name = "Andy"
age = 27
print("My name is :{0},age is:{1}.".format(name, age))
# My name is :Andy,age is:27.
#15.count():统计指定字符出现的次数
ss = " Hello World. "
print(ss.count('l')) #3
print(ss.count('a')) #0
#16.startswith():是否以参数字符开头;endswith():是否以参数字符结尾
text = "gis is cool"
text.startswith('g') #true
text.endswith('l') #true
#17.isupper():是否全大写;islower():是否全小写;
text = "gis is cool"
text.isupper() #false
text.islower() #true
#18.isdigit():是否全数字;isalpha():是否全字母;isalnum():全数字或字母
text = "gis is cool"
text.isdigit() #false
text.isalpha() #false
s = "assss"
s.isalpha() #true
s = "assss123"
s.isalnum() #true
欢迎关注我的公众号。