str 数据类型的用法
---------------------------------------------------------------------------------------------------------------------------
str:
方法: 44种
|
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill' |
a ="hello world"
|
方法 |
含义 |
实例 |
|
capitalize() |
把字符串的第一个字符大写 |
In [146]: a.capitalize() Out[146]: 'Hello world' |
|
casefold |
相当于lower() |
In[1]:'HELLO ORLD'.casefold() Out[1]: 'hello world' |
|
center |
返回一个原字符串居中,并填充至长度 width 的新字符串 |
In [3]: a.center(20,"*") Out[3]: '****hello world*****' |
|
count |
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数 |
In [4]: a.count("h") Out[4]: 1 In [7]: a.count("o",1,20) Out[7]: 2 |
|
encode encode(self, encoding='utf-8', errors='strict') |
以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace' |
In [8]: a.encode() Out[8]: b'hello world' |
|
endswith |
检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False. |
In [9]: a.endswith("d") Out[9]: True In [10]: a.endswith("d",1,9) Out[10]: False |
|
expandtabs |
定义\t的空格数 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 |
In [12]: a.expandtabs() Out[12]: 'hhh fff' In [13]: len(a) Out[13]: 7 In [14]: a.expandtabs(2) Out[14]: 'hhh fff' In [15]: len(a) Out[15]: 7 |
|
find |
检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1 |
In [16]: a ="hello world" In [17]: a.find("w") Out[17]: 6 |
|
format |
格式化字符串 |
In[20]: "{},{name}".format("hello",name="Tom") Out[20]: 'hello,Tom' |
|
format_map |
同上,很少使用 |
In [31]: b = "{name}" In[32]: b.format_map({"name":"tom"}) Out[32]: 'tom' |
|
index |
同find |
In [33]: a.index("o") Out[33]: 4 |
|
isalnum |
如果 string 至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False |
In [34]: "asdfas".isalnum() Out[34]: True In [35]: "#$%^&*(".isalnum() Out[35]: False |
|
isalpha |
如果 string 至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False |
In [36]: "asd1111".isalpha() Out[36]: False In [37]: "asd".isalpha() Out[37]: True |
|
isdecimal |
如果 string 只包含十进制数字则返回 True 否则返回 False. |
In [41]: "2131".isdecimal() Out[41]: True |
|
isdigit |
如果 string 只包含数字则返回 True 否则返回 False. |
In [44]: "asd222".isdigit() Out[44]: False In [45]: "3333".isdigit() Out[45]: True |
|
islower |
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False |
In [46]: "asdfSD".islower() Out[46]: False In [47]: "asdfas".islower() Out[47]: True |
|
isidentifier |
检查字符串是否是字母开头 |
"asdfasd".isidentifier() #true "123asdfasd".isidentifier() #false |
|
isnumeric |
检测字符串是否只由数字组成。这种方法是只针对unicode对象 |
In [48]: "asdfasd".isnumeric() Out[48]: False In [49]: "32444".isnumeric() Out[49]: True |
|
isprintable |
判断字符串中所有字符是否都属于可见字符 |
In [50]: "sdfas".isprintable() Out[50]: True In [51]: "\tasdfas".isprintable() Out[51]: False In [52]: "\nasdfas".isprintable() Out[52]: False |
|
isspace a="" b=" " if a: print("空") if b: print("空字符串") #空字符串 |
如果 string 中只包含空格,则返回 True,否则返回 False. In [56]: len("") Out[56]: 0 In [57]: len(" ") Out[57]: 1 注意:” ”空格的bool类型是True |
In [54]: "".isspace() Out[54]: False In [55]: " ".isspace() Out[55]: True In [59]: bool("") Out[59]: False In [60]: bool(" ") Out[60]: True |
|
istitle |
如果 string 是标题化的(见 title())则返回 True,否则返回 False 即每个单次的首字母为大写 |
In [62]: 'Hello World Test'.istitle() Out[62]: True In [63]: "hello world test".istitle() Out[63]: False |
|
isupper |
是否否是大写 |
In [64]: "HHHH".isupper() Out[64]: True In [65]: "HHHHh".isupper() Out[65]: False |
|
join |
以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串 |
In [69]: "*".join(['hello', 'world', 'test']) Out[69]: 'hello*world*test |
|
ljust |
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串 |
In [71]: "hhhh".ljust(20,"*") Out[71]: 'hhhh****************' In [73]: len('hhhh****************') Out[73]: 20 |
|
lower |
转换 string 中所有大写字符为小写. |
In [74]: "HHHHh".lower() Out[74]: 'hhhhh' |
|
lstrip |
截掉 string 左边 |
In [75]: " asdfa ".lstrip() Out[75]: 'asdfa ' In [76]: "***8hhhh****".lstrip("*") Out[76]: '8hhhh****' |
|
maketrans |
(⊙o⊙)… |
(⊙o⊙)… |
|
partition |
有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string. |
In [77]: "hello world test".partition(" ") Out[77]: ('hello', ' ', 'world test') |
|
Max |
返回字符串 str 中最大(小)的字符 |
In [80]: max("asdfasasd22") Out[80]: 's' In [81]: min("asdfasasd22") Out[81]: '2' |
|
Min |
||
|
replace(old, new, count=None) |
把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次. |
In [83]: "hello world".replace("l","肚子饿",2) Out[83]: 'he肚子饿肚子饿o world' |
|
rfind |
类似于find |
In [84]: "hello world".rfind("l") Out[84]: 9 |
|
rindex |
类型与index |
In [87]: "hello world".rindex("l") Out[87]: 9 |
|
rjust |
类似于ljust |
print("hello world".rjust(20,"*")) #*********hello world |
|
rpartition |
类似于partition |
/ |
|
rstrip |
类似于rstrip |
In [93]: "***8hhhh****".rstrip("*") Out[93]: '***8hhhh' |
|
Split split(self, sep=None, maxsplit=-1) |
以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串 In[104]:"hello world test".split() Out[104]: ['hello', 'world', 'test'] |
In [100]: "hello world test".split(" ",2) Out[100]: ['hello', 'world', 'test'] In [101]: "hello world test".split(" ",1) Out[101]: ['hello', 'world test'] In [102]: "hello world test".split(" ",0) Out[102]: ['hello world test'] In [103]: "hello world test".split() Out[103]: ['hello', 'world', 'test'] |
|
rsplit |
类似split() |
In [109]: "hello world te".rsplit(" ",1) Out[109]: ['hello world', 'te'] |
|
splitlines |
类似split() |
In[118]: "hello\nwold\ntest".splitlines() Out[118]: ['hello', 'wold', 'test'] |
|
startswith |
以xxx开头 |
In [120]: "hello world test".startswith("h") Out[120]: True |
|
strip |
去除两边的 |
In [121]: "******hello*******".strip("*") Out[121]: 'hello' |
|
swapcase |
翻转 string 中的大小写 |
In [124]: "sdfaHHHHs".swapcase() Out[124]: 'SDFAhhhhS' |
|
title |
转换成title类型 |
In [125]: "hello world test".title() Out[125]: 'Hello World Test' |
|
translate |
/ |
/ |
|
upper |
转成大写 |
In [136]: 'swht'.upper() Out[136]: 'SWHT |
|
zfill |
指定字符串的长度。原字符串右对齐,前面填充0 |
In [138]: 'swht'.zfill(10) Out[138]: '000000swht' |
|
In |
In [139]: "l" in "hello" Out[139]: True |
|
|
== |
/ |
In [140]: "asdf"=="asdf" Out[140]: True |
str 数据类型的用法的更多相关文章
- day01-day04总结- Python 数据类型及其用法
Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...
- str.format格式化用法(通过{}来替代%)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #str.format格式化用法(通过{}来替代%) ''' >>> help(format ...
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型, 此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...
- Redis详解(三)------ redis的五大数据类型详细用法
我们说 Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据 ...
- Python数据类型的用法
字符串的用法 res = 'hellow,world' print(res) #res.显示的都是它的方法,下划线的除外 1 判断字符串的结尾字符,返回的值的布尔形式 endswith 判断字符串的开 ...
- Redis 详解 (三) redis的五大数据类型详细用法
目录 1.string 数据类型 2.hash 数据类型 3.list 数据类型 4.set 数据类型 5.zset 数据类型 6.系统相关命令 7.key 相关命令 我们说 Redis 相对于 Me ...
- Python 数据类型及其用法
本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组 ...
- day 50 js-part1基础语法,数据类型及用法,流程控制语句,循环
js基本概念: JavaScript 是世界上最流行的脚本语言. JavaScript 被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用. JavaScript ...
- 关于python中str数据类型的内置常用方法(函数)总结
str基本数据类型常用功能 center(self,width,fllchar=none) 内容居中,width表示总长度,fllchar表示空白处默认为 ...
随机推荐
- 高次不定方程BSGS算法
学习数学真是一件赛艇的事. BSGS名字听起来非常有意思,力拔山兮气盖世,北上广深,小步大步...算法其实更有意思,它是用来求解一个方程的 \(A^x≡B mod P\) 是不是特别眼熟,有几个式子长 ...
- hdu2614 Beat
题意: 有n个问题. 给出你解决完第i个问题之后解决j问题所花的时间,花的时间越多表示难度越大,每次只能解决难度大于或等于上个题难度的问题.问你最多能解决多少问题. 他妈的,第一次做想半天想不出来如何 ...
- prettier 与 eslint 对比
Linters have two categories of rules: 代码修正一般有两种规则: Formatting rules: eg: max-len, no-mixed-spaces-an ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- javaScript - 面向对象 - ES5 和 ES6
javaScript - 面向对象 - ES5 和 ES6 ES5之前用 构造函数 构造函数的特点 就是一个普通函数, 他的函数名要大写.: 带方法的写法: 原型的方式: prototype 为内置的 ...
- php 魔术方法和魔术常量
魔术方法:PHP把类中所有以__(两个下划线)开头的方法当成魔术方法,一般建议用户不要将自定义的方法前面加上__作为前缀.魔术方法: 1. __construct() 类的默认构造方法,如果__con ...
- linux内核(四)内存管理单元MMU
1,基本概念 一个程序运行时没必要全部都同时装入内存,只需要把当前需要运行的部分装入内存即可,这样就使得一个大程序可以在较小的内存中运行,也使得内存中可以同时装入更多的程序并发执行,从用户角度看,该系 ...
- sql server 与 mysql在自定以数据类型的区别
sql server 中可以使用 create TYPE postal_code FORM varchar(6) not null; 用于限定邮编的数据位数,他基于varchar数据类型 注意: ...
- Ubuntu(Linux Mint):sudo apt-get upgrade升级失败
Ubuntu上进行sudo apt-get upgrade后出现异常,升级失败. 异常信息如下: E: dpkg was interrupted, you must manually run 'dpk ...
- WinServer-AD域控入门
计算机账户和用户账户的区别 域控中不需要事先建立计算机账户,但必须建立登录用户账户. 计算机只要知道域控管理员或者授权管理账户,就可以利用此账户为所有计算机加域. 计算机加域成功之后,都会在AD管理里 ...