Python 运维大数据 爬虫Python AI Sql程序猿日记

Python字符串常用操作

2017-12-10  本文已影响49人  cjl72513

下面操作所使用的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
上一篇 下一篇

猜你喜欢

热点阅读