python字符串内置方法总结

#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

欢迎关注我的公众号。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遥感与地理信息

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值