Python字符串常用操作

作者: cjl72513 | 来源:发表于2017-12-10 17:14 被阅读49次

下面操作所使用的name的值如下

name='jupyter notebook'
name

大小写相关

capitalize()

这一行首字母转换为大写

print(name)
print(name.capitalize())
jupyter notebook
Jupyter notebook

title()

所有单词的首字母都大写

print(name.title())
Jupyter Notebook

casefold()

全部转换为小写

print("BOOK".casefold())
book

lower()

全部转换为小写

print("BOY".lower())
boy

upper()

全部转换为大写

print("boy".upper())
BOY

swapcase()

把大写转小写,小写转大写

print("Jared Chen".swapcase())
jARED cHEN

istitle()

判断首字母是否全部为大写

print("Jared Chen".istitle())
print("Jared chen".istitle())
True
False

isupper()

判断是否全部大写

print("JARED CHEN".isupper())
print("JARED CHENa".isupper())
True
False

查找相关

find()

查找字符串中包含的指定字符,并返回字符串中最左边的指定字符的下标

print(name.find('up'))
print(name[name.find('up'):])
1
upyter notebook

rfind()

从左往右数,找到最右边那个值的下标

print("jared chen".rfind('e'))
8

判断相关

isalnum()

判断字符串中是不是同时包含字母和数字,如果同时包含了字母和数字,而且没有包含其它空格和任何特殊字符那么就返回True

print('123aBc'.isalnum())
print('.123aBc'.isalnum())
True
False

isalpha()

判断字符串中是不是只有英文字母,如果只有英文字母而其没有其它数字、空格和任何特殊字符,就返回True

print('123aBc'.isalpha())
print('aBc'.isalpha())
False
True

isdecimal()

判断是否为十进制

print("123".isdecimal())
print("0x123".isdecimal())
print("abc".isdecimal())
True
False
False

isdigit()

判断是否为整数

print("123".isdigit())
print("123.2".isdigit())
True
False

isnumeric()

判断是否只包含数字

print("123.2".isnumeric())
print("123".isnumeric())
False
True

isspace()

判断字符串是否为空格

print("12 34".isspace())
print(" ".isspace())
False
True

endswith()

判断字符串是否以指定字符结尾,如果是,就返回True

print(name.endswith("an"))
print(name.endswith("ok"))
False
True

startswith()

判断字符串是否以指定字符开始,如果是,就返回True

print(name.startswith("an"))
print(name.startswith("ju"))
False
True

打印格式相关

format()

name = "my \tname is {name}, age is {age}."
name
'my \tname is {name}, age is {age}.'

print(name.format(age=22, name="jared"))
print(name.format_map({'age':22, 'name':'jared'}))
my  name is jared, age is 22.
my  name is jared, age is 22.

join()

list = ["1","2","3","4","5"]
print("+".join(list))
print(" ".join(list))
1+2+3+4+5
1 2 3 4 5

ljust()

打印100个字符,不够的话右边的全部用指定字符来填补,这里用*

print(name.ljust(100,"*"))
my  name is {name}, age is {age}.*******************************************************************

rjust()

打印100个字符,不够的话左边的全部用指定字符来填补,这里用*

print(name.rjust(100,"*"))
*******************************************************************my   name is {name}, age is {age}.

center()

print(name.center(50,'-'))
--------my  name is {name}, age is {age}.---------

替换相关

expandtabs()

把字符串中的tab转换成多个空格,这里转换成了30个空格

name = "jupyter\tnotebook"
print(name.expandtabs(30))
print(name.expandtabs(tabsize=30))
jupyter                       notebook
jupyter                       notebook

replace()

替换字符串中的指定字符,可以指定替换次数

print("jared chen".replace('e','E',1))
print("jared chen".replace('e','E'))
jarEd chen
jarEd chEn

其他常用操作

split()

把字符串按照指定字符分成一个列表,默认以空格分割成一个列表

print("jared+chen+".split("+"))
['jared', 'chen', '']

splitlines()

按照换行符,把字符串分割成一个列表

print("boy\njared\n".splitlines())
['boy', 'jared']

lstrip()

去除左边的换行

print("\n1 23\n".lstrip())
1 23

rstrip()

去除右边的换行

print("\n1 23\n".rstrip())
1 23

strip()

去除两边的换行

print("\n1 23\n".strip())
1 23

count()

print(name.count('o'))
3

encode()

print(name.encode())
b'jupyter\tnotebook'

打印python目前的编码模式

import sys
print(sys.getdefaultencoding())
utf-8

相关文章

  • Python字符串、列表、元组切片操作,以及字符串的常用操作

    本文记录了python中字符串常用的切片操作,该操作也适用于列表、元组。同时记录了字符串的常用操作。 结束

  • Python中的变量分类以及常用操作

    Python的变量分类: 列表、元组、字典、字符串常用操作

  • Python 学习总结——字符串

    1 Python 字符串的 CRUD 操作 1.1 创建字符串 字符串是 Python 中最常用的数据类型。我们可...

  • 数据序列

    字符串 目标 认识字符串 下标 切片 常用操作方法 一. 认识字符串 字符串是 Python 中最常用的数据类型。...

  • Python常用对象之字符串操作手册

    Python常用对象之字符串操作手册 详细介绍操作字符串的内置函数、运算符、字符串方法、特殊字符串等 因为字符串是...

  • Python中的字符串

    Python中的字符串 对Python中的字符串常用函数的笔记, 方便查用. 格式化操作符% %通过匹配不同的字符...

  • Python常用语法操作

    以下列出在时间过程中常用到的几种 Python 脚本操作以及相关功能实现脚本。 一、字符串操作 描述:包括字符串拼...

  • Python常用语法二

    Python 字符串操作和文件操作以及其它Python能力补充 Python字符串操作 in和not in: 'x...

  • 12 Python字符串

    字符串的基本操作 字符串是Python中最常用的数据类型。我们可以使用引号('或")创建字符串。创建字符串很简单,...

  • Python起步——列表

    python列表的常用操作 1. list函数 将元组和字符串转化为列表。 2. 基本列表操作 2.1 元素赋值 ...

网友评论

    本文标题:Python字符串常用操作

    本文链接:https://www.haomeiwen.com/subject/ahevixtx.html