python字符串-方法

一、
1. upper()
作用:将字符串中字符转换为大写

In [17]: spam
Out[17]: ‘hello,world‘

In [18]: print(spam.upper())
HELLO,WORLD

2.lower()
作用:将字符串中字符转换为小写

In [19]: spam = spam.upper()

In [20]: spam
Out[20]: ‘HELLO,WORLD‘

In [21]: print(spam.lower())
hello,world

3.isupper()
作用:判断字符串中是否有大写字符

In [22]: spam
Out[22]: ‘HELLO,WORLD‘

In [23]: spam.isupper()
Out[23]: True

4.islower()
作用:判断字符串中是否有小写字符

In [24]: spam
Out[24]: ‘HELLO,WORLD‘

In [25]: spam.islower()
Out[25]: False

5.isalpha()
isalpha()返回 True,如果字符串只包含字母,并且非空

In [27]: spam
Out[27]: ‘HELLO,WORLD‘

In [28]: spam.isalpha()
Out[28]: False

6.isalnum()
isalnum()返回 True,如果字符串只包含字母和数字,并且非空

In [39]: mystring01 = ‘123hello‘

In [40]: mystring01.isalnum()
Out[40]: True

7.isdecimal()
isdecimal()返回 True,如果字符串只包含数字字符,并且非空

In [46]: mystring01 = ‘123456‘

In [47]: mystring01.isdecimal()
Out[47]: True

8.isspace()
isspace()返回 True,如果字符串只包含空格、制表符和换行,并且非空

In [53]: mystring01 = ‘\n ‘

In [54]: mystring01.isspace()
Out[54]: True

9.istitle()
istitle()返回True,如果字符串仅包含以大写字母开后面都是小写字母的单词

In [62]: mystring01 = ‘Helloworld‘

In [63]: mystring01.istitle()
Out[63]: True

10.startswith()
startswith()方法返回 True,如果它们所调用的字符串以该方法传入 的字符串开始

In [71]: ‘hello world‘.startswith(‘hello‘)
Out[71]: True

11.endswith()
endswith()方法返回 True,如果它们所调用的字符串以该方法传入 的字符串开结束

In [72]: ‘hello world‘.endswith(‘ld‘)
Out[72]: True

12.join()
拼接字符串列表

In [76]: ‘-‘.join([‘aaa‘,‘bbb‘,‘ccc‘])
Out[76]: ‘aaa-bbb-ccc‘

13.split()
分解字符串

In [79]: ‘aaa-bbb-ccc‘.split("-")
Out[79]: [‘aaa‘, ‘bbb‘, ‘ccc‘]

14.strip()
(1)删除两侧空白字符

In [86]: print(mystring01)
  hel lo , w o r ld

In [87]: print(mystring01.strip())
hel lo , w o r ld

(2)删除指定字符串

In [108]: print(mystring01)
  hel lo , w o r ld

In [109]: print(mystring01.strip(‘ hel lo‘))
, w o r ld

15.lstrip()
删除左边空白字符

In [89]: print(mystring01)
  hel lo , w o r ld

In [90]: print(mystring01.lstrip())
hel lo , w o r ld

16.rstrip()
删除右边空白字符

In [92]: print(mystring01)
  hel lo , w o r ld

In [93]: print(mystring01.rstrip())
  hel lo , w o r ld

17.pyperclip模块
使用pyperclip模块总的copy和paste参数

import pyperclip
pyperclip.copy(‘Hello world!‘)
pyperclip.paste()

原文地址:https://www.cnblogs.com/dingkailinux/p/8461407.html

时间: 2024-08-01 09:32:39

python字符串-方法的相关文章

Python 字符串方法详解

Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几乎90% 以上的代码都是关于整数或字符串操作,所以与整数一样,Python 的字符串实现也使用了许多拿优化技术,使得字符串的性能达到极致.与 C++ 标准库(STL)中的 std::string 不同,python 字符串集合了许多字符串相关的算法,以方法成员的方式提供接口,使用起来非常方便. 字符

python 字符串方法积累

字符串方法 str.strip([chars]) 返回字符串的副本,删除前导符和尾随符,chars参数为需要被删除的字符集,默认情况下删除空格,chars参数不是前缀或后缀,而是所有chars参数中的组合都被删除 例如: >>> 'www.example.com'.strip('cmowz.') 'example' >>> '   spacious   '.strip() 'spacious' str.join(iterable) 通过iterable将字符串串联起来并

几个Python字符串方法浅析

字符串提供了一系列的方法去实现复杂的文本处理任务.方法就是与特定的对象关联在一起的函数.方法调用同时进行了两次操作: 第一次:属性读取--具有object.attribute格式的表达式可以理解为"读取object对象的属性attribute的值": 第二次:函数调用表达式--具有函数(参数)格式的表达式意味着"调用函数代码,传递零或者更多用逗号隔开的参数对象,最后返回函数的返回值". 方法调用表达式对象,方法(参数)从左至右运行,也就是说Python首先读取对象方

Python字符串方法

方法 描述 string.capitalize( ) 返回首字母大写的字符串的副本 string.center(width[, fillchar]) 返回一个长度为max(len(string), width)且其中String的副本居中的字符串,两侧使用fillchar(默认为空字符)填充 string.count(sub[, start[, end]]) 计算子字符串sub的出现次数,可将搜索范围限制为string[start:end] string.decode([encoding[,er

python 字符串方法及列表,元组,字典(一)

字符串 str 注: 若想要保持单引号和双引号为字符串的一部分 1)单双引号交替使用, 2)使用转义字符\ 3)成对三个引号被存在变量里 二.字符串详细用法 字符串的单个取值例 p_1=”hello” 字符串元素定位置,通过索引 正序(→)  反序(←)开始值从0开始,反序从-1开始 取值方式  字符串名[索引值] 例print(p_1[-1])  取o ①字符串切片:字符串名[m:n:k]m:索引起始位置,n:索引结束位置+1,k:步长 默认值为1,取左不取右 例 1,print(p_1[2:

python字符串方法之查找

str.find() str.rfind() [作用:类似index,查找字符] [英语:r=>right|右边,find=>寻找] [说明:返回一个新的整数,查找到的位置,找不到出现-1,index找不到要报错] In [190]: "The stars are not afraid to appear like fireflies.".find("vition")#不存在返回-1 Out[190]: -1 In [192]: "The st

python字符串方法学习笔记

# 一.字符串大小写转换# 字符串首字符大写print("hello world".capitalize())# 将字符串变为标题print("hello WORLD".title())# 将字符串转为大写print("hello world".upper())# 把字符串转为小写print("HELLO WORLD".lower())# 翻转字符串中的大小写print("hello WORLD".swap

Python字符串解析方法汇总

Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: Text Text 2.center  将字符串居中  ljust(从左到右填充),rjust(从右到左填充) a='test' print(a.center(20,'_')) print(a.rjust(20,'_')) 结果: ________test________ _____________

Python 字符串中 startswith()方法

Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. str.startswith(str, beg=0,end=len(string)); #!/usr/bin/python str = "this is string example....wow!!!"; print str.startswith( 'this' ); print str.star