Python中 str类方法(1)

capitalize()  字符串首字母大写

1 str1="ni hao ma"
2 str2=str1.capitalize()
3 print(str2)#输出: Ni hao ma

center(width, fillchar=None)  将字符串放在中间;在制定长度下,首尾以指定字符填充

str1="this"
str2=str1.center(20,"*")
print(str2)

#输出:********this********

str1="this"
str2=str1.center(5,"*")
print(str2)
#输出:*this

str1="this"
str2=str1.center(6,"*")
print(str2)
#输出:*this*

count(sub, start=None, end=None)  计算某字符在字符串中的数量

str1="this is string"
num=str1.count("i")
print(num)

#输出:3

decode(encoding=None, errors=None):解码

encode(self, encoding=None, errors=None):编码

endswith(self, suffix, start=None, end=None)  判断是否以某字符结尾

str1="this is string"
num=str1.endswith("ing")
print(num)

#输出:True

expandtabs(self, tabsize=None)   返回制表符,tabsize此选项指定要替换 为制表符符"/h"的字符数    默认为8

str1=‘this\tis\ta\tstring.‘
str2=str1.expandtabs()
print(str2)

#输出:this    is      a       string.

find(self, sub, start=None, end=None)  在字符串中寻找指定字符的位置

str1=‘this is a string.‘
#找到的时候
num=str1.find("h")
print(num)

#输出:1

#找不到的时候 返回-1
num=str1.find("x")
print(num)
#输出:-1

format(*args, **kwargs)  类似%s的用法,它通过{}来实现

str1=‘my name is {0},Age {1}‘
num=str1.format("wang","26")
print(num)

#输出:my name is wang,Age 26

index(self, sub, start=None, end=None)  类似find  但是如果找不到的情况下,程序会报错

isalnum(self)  判断字符串中是否都是数字和字母,如果是返回True,相反返回false  ;注意空格 符号 都会返回false ; 只有数字 或者只有字母 返回True

str1=‘my name is {0},Age {1}‘
num=str1.isalnum()
print(num)

#输出:false

str1=‘1aareawr23‘
num=str1.isalnum()
print(num)

#输出:True

isalpha(self)  判断字符串中是否都是字母,如果是则返回True,否则返回False

isdigit(self)  判断字符串中是否都是数字,如果是则返回True,否则返回False

islower(self)  判断字符串中的字母是否都是小写,如果是返回True,否则返回False

 1 str1=‘wasd fdfdsa‘
 2 num=str1.islower()
 3 print(num)
 4
 5 #输出:True
 6
 7 str1=‘lAO wang‘
 8 num=str1.islower()
 9 print(num)
10
11 #输出:Flase

isspace(self)  判断字符串是否都是空格;如果是返回True;否则返回false

string = ‘ ‘
new_str = string.isspace()
print(new_str)
#输出:True
string = ‘My name is Yue,my age is 18.‘
new_str = string.isspace()
print(new_str)
#输出:False

istitle(self)  判断字符串中所有的单词首字母是否都是大写, 如果是返回True ,否则返回false

string = ‘My Name Is Yue.‘
new_str = string.istitle()
print(new_str)
#输出:True
string = ‘My name is Yue,my age is 18.‘
new_str = string.istitle()
print(new_str)
#输出:False

isupper(self)  判断字符串中所有字母是否是大写   是返回True   不是返回false

string = ‘MY NAME IS YUE.‘
new_str = string.isupper()
print(new_str)
#输出:True
string = ‘My name is Yue.‘
new_str = string.isupper()
print(new_str)
#输出:False

join(self,iterable)  将序列中的元素以指定的字符连接生成一个新的字符串

string = ("haha","lala","ohoh")
str = "-"
print(str.join(string))
#输出:haha-lala-ohoh

lower(self)  转换字符串中所有大写字符为小写。

string = "My Name is YUE."
print(string.lower())
# 输出:my name is yue.

ljust(self, width, fillchar=None)  返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

string = "My name is wang."
print(string.ljust(18))
#输出:My name is wang.                          

lstrip(self, chars=None)  截掉字符串左边的空格或指定字符

string = " My Name is YUE."
print(string.lstrip())
#输出:My Name is YUE.
string = "My Name is YUE."
print(string.lstrip(‘My‘))
#输出: Name is YUE.

partition(self, sep)    根据指定的分隔符将字符串进行分割

string = "http://www.mingyuanyun.com"
print(string.partition(‘://‘))
#输出:(‘http‘, ‘://‘, ‘www.mingyuanyun.com‘)

replace(self, old, new, count=None)  把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

string = "My name is yue."
print(string.replace("yue","ying"))
#输出:My name is ying.

split(self, sep=None, maxsplit=None)  通过指定分隔符对字符串进行切片。

string = "haha lala gege"
print(string.split(‘ ‘))
#输出:[‘haha‘, ‘lala‘, ‘gege‘]
print(string.split(‘ ‘, 1 ))
#输出: [‘haha‘, ‘lala gege‘]

swapcase(self)  对字符串的大小写字母进行转换。

string = "My Name Is Yue."
print(string.swapcase())
#输出:mY nAME iS yUE.

title(self)  返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。

string = "my name is yue,my age is 18."
print(string.title())
#输出:My Name Is Yue,My Age Is 18.

upper(self)  将字符串中的小写字母转为大写字母。

string = "my name is yue,my age is 18."
print(string.upper())
#输出:MY NAME IS YUE,MY AGE IS 18.

zfill(self, width)  返回指定长度的字符串,原字符串右对齐,前面填充0。

string = "my name is yue."
print(string.zfill(18))
#输出:000my name is yue.

translate(self, table, deletechars=None)  根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。

from string import maketrans
str = "aoeiu"
num = "12345"
trantab = maketrans(str, num)
string = "my name is yue"
print(string.translate(trantab))
# 输出:my n1m3 4s y53
时间: 2024-08-10 23:22:22

Python中 str类方法(1)的相关文章

Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因

python进阶三(面向对象编程基础)【3-4 python中定义类方法】

python中定义类方法 和属性类似,方法也分实例方法和类方法. 在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身. 要在class中定义类方法,需要这么写: 1 class Person(object): 2 count = 0 3 @classmethod 4 def how_many(cls): 5 return cls.count 6 def __init__(self, name): 7 self.name = name 8 Person.count = P

Python中str()和repr()的区别

Python中str()和repr()的区别 区别 其实用处就是最大的区别了:str()主要用来为终端用户输出一些信息,而repr()主要用来调试:同时后者的目标是为了消除一些歧义(例如浮点数的精度问题),前者主要为了可读. 使用 In [12]: s = 'abc' In [13]: print(str(s)) abc In [14]: print(2.0/11) 0.18181818181818182 In [15]: repr(s) Out[15]: "'abc'" In [16

【开发者笔记】python中的类方法(@classmethod)和静态方法(@staticmethod)

在java.c#等高级语言中我们用static来定义静态方法和静态变量,那么在python中如何定义静态方法和静态变量呢. python提供了@classmethod和@staticmethod来定义静态方法,刚接触的时候不太明白,Stack Overflow提供了一个比较方便理解的解释,Stack Overflow回答. 但是看完还是不太理解,于是自己写了个实例: class stclass(): d=1 #对象方法 def imethod(self): print(self) print("

Python中str()与repr()函数的区别

在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因为提供两个功能完全相同的内建函数是没有意义的.先看一个例子. >>> print(str('123')) 123 >

Python中的类方法、实例方法、静态方法

类方法 @classmethod 在python中使用较少,类方法传入的第一个参数是 cls,是类本身: 类方法可以通过类直接调用或者通过实例直接调用,但无论哪种调用方式,最左侧传入的参数一定是类本身. 通常情况下,类方法使用 @classmethod 装饰器来声明 实例方法 实例方法需要将类实例化后调用,如果使用类直接调用实例方法,需要显式的将实例作为参数传入:使用实例调用则不需要. 最左侧传入的参数 self,是实例本身. 静态方法 @staticmethod 指类中无需实例参与即可调用的方

Python中的类方法及属性总结举例,编写memcached启动脚本举例

1.类的属性总结类属性,也是公有属性, 类的私有属性, 对象的共有属性, 对象的私有属性, 内置属性, 函数的局部变量, 全局变量, #/usr/bin/env python # -*- coding:utf-8 -*- class MyClass(object): var1 = '类属性,类的公有属性 var1' __var2 = '类的私有属性 __var2' def func1(self): self.var3 = '对象的公有属性 var3' self.__var4 = '对象的私有属性

Python中str.replace()的使用方法

Example: target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'   #today 被定义为根目录+今日时间,同理,now定义为为此时时间,例如为09.01.16:34:00则Linux中 target=根目录+0901/163300,将comment中的‘ ’替换为'_' Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,

Python 中 创建类方法为什么要加self

Python的类方法和普通的函数有一个明显的区别,在类的方法必须有一个额外的第一个参数(self),但在调用这个方法的时候不必为这个参数数值(显胜于隐的引发).在Python的类方法中这个特别的参数指代是对象本身.而按照Python的惯例,它用self来表示(也可那以用其他任何名称来代替,只是 规范和标准建议一致使用self) 注:self在Python里不是关键字,self代表当前对象的地址,self能避免非限定调用的全局变量.