【Python】str类方法说明
#capitalize():字符串首字符大写
string = 'this is a string.'
new_str = string.capitalize()
print(new_str)
#输出:This is a string.
#center(width, fillchar=None):将字符串放在中间,在指定长度下,首尾以指定字符填充
string = 'this is a string.'
new_str = string.center(30,'*')
print(new_str)
#输出:******this is a string.*******
#count(sub, start=None, end=None):计算字符串中某字符的数量
string = 'this is a string.'
new_str = string.count('i')
print(new_str)
#输出:3
#decode(encoding=None, errors=None):解码
string = 'this is a string.'
new_str = string.decode()
print(new_str)
#encode(self, encoding=None, errors=None):编码
string = 'this is a string.'
new_str = string.encode()
print(new_str)
#endswith(self, suffix, start=None, end=None):判断是否以某字符结尾
string = 'this is a string.'
new_str = string.endswith('ing.')
print(new_str)
#输出:True
new_str = string.endswith('xx')
print(new_str)
#输出:False
#expandtabs(self, tabsize=None):返回制表符。tabsize此选项指定要替换为制表符“\t' 的字符数,默认为8
string_expandtabs = 'this\tis\ta\tstring.'
new_str = string_expandtabs.expandtabs()
print(new_str)
#输出:this is a string.
#find(self, sub, start=None, end=None):在字符串中寻找指定字符的位置
string = 'this is a string.'
new_str = string.find('a') #找的到的情况
print(new_str)
#输出:8
new_str = string.find('xx') #找不到的情况返回-1
print(new_str)
#输出:-1
#format(*args, **kwargs):类似%s的用法,它通过{}来实现
string1 = 'My name is {0},my job is {1}.'
new_str1 = string1.format('yue','tester')
print(new_str1)
#输出:My name is yue,my job is tester.
string2 = 'My name is {name},my job is {job}.'
new_str2 = string2.format(name='yue',job='tester')
print(new_str2)
#输出:My name is yue,my job is tester.
#index(self, sub, start=None, end=None):;类似find
string = 'this is a string.'
new_str = string.index('a') #找的到的情况
print(new_str)
#输出:8
new_str = string.index('xx') #找不到的情况,程序报错
print(new_str)
#输出:程序运行报错,ValueError: substring not found
#isalnum(self):判断字符串中是否都是数字和字母,如果是则返回True,否则返回False
string = 'My name is yue,my age is 18.'
new_str = string.isalnum()
print(new_str)
#输出:False
string = 'haha18121314lala'
new_str = string.isalnum()
print(new_str)
#输出:True
#isalpha(self):判断字符串中是否都是字母,如果是则返回True,否则返回False
string = 'abcdefg'
new_str = string.isalpha()
print(new_str)
#输出:True
string = 'my name is yue'
new_str = string.isalpha() #字母中间带空格、特殊字符都不行
print(new_str)
#输出:False
# isdigit(self):判断字符串中是否都是数字,如果是则返回True,否则返回False
string = '1234567890'
new_str = string.isdigit()
print(new_str)
#输出:True
string = 'haha123lala'
new_str = string.isdigit() #中间带空格、特殊字符都不行
print(new_str)
#输出:False
# islower(self):判断字符串中的字母是否都是小写,如果是则返回True,否则返回False
string = 'my name is yue,my age is 18.'
new_str = string.islower()
print(new_str)
#输出:True
string = 'My name is Yue,my age is 18.'
new_str = string.islower()
print(new_str)
#输出:False
# 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):检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
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):检测字符串中所有的字母是否都为大写。
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
# ljust(self, width, fillchar=None):返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
string = "My name is yue."
print(string.ljust(18))
#输出:My name is yue.
# lower(self):转换字符串中所有大写字符为小写。
string = "My Name is YUE."
print(string.lower())
# 输出:my name is yue.
# 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.
# rfind(self, sub, start=None, end=None):返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
string = "My name is yue."
print(string.rfind('is'))
#输出:8
string = "My name is yue."
print(string.rfind('XXX'))
#输出:-1
# rindex(self, sub, start=None, end=None):返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
string = "My name is yue."
print(string.rindex('is'))
#输出:8
string = "My name is yue."
print(string.rindex('XXX'))
#输出:ValueError: substring not found
# rjust(self, width, fillchar=None):返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。
string = "My name is yue."
print(string.rjust(18))
#输出: My name is yue.
# rpartition(self, sep):根据指定的分隔符将字符串从右进行分割。
string = "http://www.mingyuanyun.com"
print(string.rpartition('.'))
#输出:('http://www.mingyuanyun', '.', 'com')
# split(self, sep=None, maxsplit=None):通过指定分隔符对字符串进行切片。
string = "haha lala gege"
print(string.split(' '))
#输出:['haha', 'lala', 'gege']
print(string.split(' ', 1 ))
#输出: ['haha', 'lala gege']
# rsplit(self, sep=None, maxsplit=None):通过指定分隔符对字符串从右进行切片。
string = "haha lala gege"
print(string.rsplit(' '))
#输出:['haha', 'lala', 'gege']
print(string.rsplit(' ', 1 ))
#输出: ['haha lala', 'gege']
# rstrip(self, chars=None):删除 string 字符串末尾的指定字符(默认为空格).
string = " My name is yue. "
print(string.rstrip())
#输出: My name is yue.
# strip(self, chars=None):移除字符串头尾指定的字符(默认为空格)。
string = " My name is yue. "
print(string.strip())
#输出:My name is yue.
# splitlines(self, keepends=False):按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
str1 = 'ab c\n\nde fg\rkl\r\n'
print(str1.splitlines())
# 输出:['ab c', '', 'de fg', 'kl']
str2 = 'ab c\n\nde fg\rkl\r\n'
print(str2.splitlines(True))
# 输出:['ab c\n', '\n', 'de fg\r', 'kl\r\n']
# startswith(self, prefix, start=None, end=None):检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
string = "My name is yue. "
print(string.startswith('My'))
#输出:True
string = "My name is yue."
print(string.startswith('yue'))
#输出:False
# 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.
# 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
# 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.
【Python】str类方法说明的更多相关文章
- 关于python的类方法、实例方法和静态方法区别
python的类方法需要在方法前面加装饰器:@classmethod ,静态方法是在方法前面加装饰器:@staticmethod. 类方法.类属性是属于类自身,属于类自身的命名空间,和实例方法.实例属 ...
- #python str.format 方法被用于字符串的格式化输出。
#python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...
- Python str() 函数
Python str() 函数 Python 内置函数 描述 str() 函数将对象转化为适于人阅读的形式. 语法 以下是 str() 方法的语法: class str(object='') 参数 ...
- Python str 与 bytes 类型(Python2/3 对 str 的处理)
本文均在 Python 3 下测试通过,python 2.x 会略有不同. 1. str/bytes >> s = '123' >> type(s) str >> ...
- Python str & repr
Python str & repr repr 更多是用来配合 eval 的 (<- 点击查看),str 更多是用来转换成字符串格式的 str() & repr() str() 和 ...
- Python str方法总结
1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string 1 2 3 4 >>>a = 'shaw' >>> ...
- python str()与repr()
相同点: 将任意值转为字符串 不同点: str()致力于生成一个对象的可读性好的字符串表示,它的返回结果通常无法用于eval()求值,但很适合用于print语句输出 repr()出来的值是给pytho ...
- Python str字符串常用到的函数
# -*- coding: utf-8 -*- x='pythonnnnnnoooo' print type(x) # <type 'str'> 输出类型 print x.capitali ...
- python str.format()
python中的字符串格式函数str.format(): #使用str.format()函数 #使用'{}'占位符 print('I\'m {},{}'.format('Hongten','Welco ...
随机推荐
- beat your own python env
1,进入根目录,修改.bashrc,增加一个PATH目录 例如:alias cjtf='export PATH=/home/www/xxx/python_env:$PATH' 如果个人的机器的就不用a ...
- 【数据类型】Dictionary 与 ConcurrentDictionary 待续
Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, T ...
- sql sever跨数据库复制数据的方法【转】
1,用Opendatasource系统函数 详细的用法已经注释在sql代码中了.这个是在sqlserver到sqlserver之间的倒数据.2005,2008,2012应该都是适用的. --从远程服务 ...
- 华为地铁换乘 Java
public class MetroTransfor { static int ver=37; static int point=35; static int [][] di ...
- mysql数据库使用
C#操作Mysql数据库的存储过程,网址 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(date1,date2) date1 和 date2 参数是合法的日期或日期/时间 ...
- 理解js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性__proto ...
- bzoj 3172 单词 ac自动机|后缀数组
题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...
- 《Pro Express.js》学习笔记——Express框架常用设置项
Express 设置 系统设置 1. 无须再定义,大部分有默认值,可不设置 2. 常用设置 env view cache view engine views trust pro ...
- powershell中使用超大内存对象
powershell中使用超大内存对象 简单介绍了powershell中超大内存对象的用途,开启powershell超大内存对象的办法. powershell 传教士 原创文章 2016-12-31 ...
- 基于H5的移动端开发,window.location.href在IOS系统无法触发问题
最近负责公司的微信公众号开发项目,基于H5进行开发,某些页面window.location.href在Android机上能正常运行而IOS系统上无法运行,导致无法重定向到指定页面,查了好久终于找到方法 ...