温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python字符串方法

发布时间:2021-09-03 14:22:38 来源:亿速云 阅读:135 作者:chen 栏目:编程语言

本篇内容介绍了“Python字符串方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

格式化字符串

  • split()

将字符串分割成列表,默认以空格为分隔符

a="you can't see mee"
a.split()             #输出内容为 ["you","can't","see","me"]
a.split(" ' ")        #输出内容为['you cant','t see me']
  • strip()

用于移除字符串两端的字符 ;当括号为空时候,默认删除空白符(包括'\n', '\r', '\t', ' ')

a="        123"
a.strip()                        #输出内容为 "123" ,注意前面有a=" 123"前面空格

例子2:

a= "0000000this is string example....wow!!!0000000"
print a.strip("0")

#以上实例输出结果如下:
this is string example....wow!!!

特别说明: 只要删除内容存在,不论顺序正反都一样 如strip("12") 和strip("21"),如下所示

c="123acb"
c.strip("12")                #输出内容为"3abc"
c.strip("21")                #输出内容一样为"3abc"

strip()总结:

python的strip函数有两种用法:一般去首尾

  • 如果省略参数,那么将会执行去除两端空格。如:

 str="   abc   "  
print(str.strip()) #结果为abc
  • 如果传入了参数,那么将按照字符在两端去除相应字符,但这时候和空格没有任何关系。

str="  abc "  
print(str.strip(" a")) #输出"bc"  
print(str.strip("ac")) #输出" abc " 什么都没做  
print(str.strip("a")) #输出" abc "什么都没做

join()

将 字符串,列表,字典,元组中的元素链接成新的字符串

a="123"
" |".join(a)
>>'1 |2 |3'
b=['a','b','c']
" ".join(b)
>>'a b c'
c=('i','j','k')
"_".join(c)
>>'i_j_k'
s = {"name":"lee","age":18}
"_".join(s)
>>'name_age'

注意:列表,元组等序列里面的内容必须是字符串,否则会报错

replace(old, new,替换次数)

字符串替换,第一个参数旧字符串,第二个要替换的字符串,第三个替换的次数,可为空默认全部替换

s = "hello python python python"
print(s.replace("python", "java"))
print(s.replace("python", "java",2))

#输出
hello java java java
hello java java python

find()

检测字符串中是否包含子字符串 str 查找内容在第几个字符,不存在返回**-1**

a="you can't see me"
a.find("you")                 #输出内容为0
afind("can't")               #输出内容为4
a.find("asd")               #输出内容为-1

index()

检测字符串中是否包含子字符串 str
用法和find() 差不多,不过如果查找内容不存在,返回一个错误,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内

a="you can't see me"
a.index("you")            #输出内容为0
a.index("can't")          #输出内容为4
a.index("asd")           
# 输出内容为:
 Traceback (most recent call last):
  File "<stdin>", line 1,in <module>
ValueError: substring not found
```
如果参数出现很多次,要如何做呢?

例2:

```
t=tuple('Allen')
print(t)
#输出 ('A', 'l', 'l', 'e', 'n')
a=t.index('l',2)
print(a)
#输出2
```
因为第一个’l’的出现位置是1,所以我们将开始索引加1继续寻找,果然,在索引为2的位置又找到了’l’。

seek()

seek()函数是属于文件操作中的函数,用来移动文件读取指针到指定位置。
语法:

fileObject.seek(offset[, whence])
#offset – 开始的偏移量,也就是代表需要移动偏移的字节数
#whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始

#算起,1代表从当前位置开始算起,2代表从文件末尾算起。

upper()

转换成大写

s='abc'
s.upper()
#输出ABC

lower()

转换成小写

swapcase()

大写字母转换成小写,小写字母转换成大写

capitalize()

把字符串中,第一个字符转换成大写,其余转换成小写

title()

把字符串中,每个单词的首字母改成大写

注意:以上方法不改变原来的字符串,产生一个新的字符串

案列:

s= "heLlO World"
a=s.swapcase()
b=s.capitalize()
c=s.title()
d = s.upper()
e = s.lower()
print("swapcase:",a)
print("capitalize:",b)
print("title:",c)
print("upper:",d)
print("lower:",e)
print(s.isalpha())
print(s)

#输出
swapcase: HElLo wORLD  
capitalize: Hello world
title: Hello World     
upper: HELLO WORLD     
lower: hello world     
False #注意有空格不算字母
heLlO World

isalpha()

是否全部为字母,注意:有空格就不算字母了,中文字符串算字母

s= "heLlO World"
print(s.isalpha())
s1 = "hello"
print(s1.isalpha())
print("张三".isalpha())
print("张三1".isalpha())

#输出
False
True
True
False

isnumeric()

是否全部由数字组成

注意:中文数字,罗马数字,字符串数字,还有输入法中的算做数字的特殊符号都是算是数字。

英文数字不算

print("1234".isnumeric())
print("一二34".isnumeric())
print("一二".isnumeric())
print("one".isnumeric())  #英文不是
print("ⅠⅡⅢⅣ".isnumeric())
print("㈠".isnumeric())

#输出
True
True 
True 
False
True 
True

特殊数字符号

print("❶".isnumeric())
print("①".isnumeric())
print("⒈".isnumeric())  #注意这里不是一点,输入法特殊符号中数字 
print("⒒".isnumeric())
print("⑴".isnumeric())
print("⑾".isnumeric())

True
True
True
True
True
True

Python字符串方法

isalnum()

是否全部由字母数字组成

注意:输入法中的特殊符号算数字,常见标点符号不算字母数字如 !等

“Python字符串方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI