Python 基础 —— str
- 大小写转换: str.lower() / str.upper()
- 数字还是字符:str.isalnum() (字母或数字,且至少一个字符) / str.isalpha() / 是不是空格:c.isspace()
- str(i).zfill(5):不足补零,以使长度一致;
- str(1).zfill(3):‘001’
- strip([chars]):接受参数的strip:
- “hello wo!rld!!!”.strip("!") ⇒ “hello wo!rld”
- 只会删除首尾的
!
,不会处理字符串中间的;
1. 常用成员函数
split() 与 strip()
二者常结合使用:
s.strip().split(...)
strip() 删除首位两端的空格,注意仅删除首尾两端;
S.split(sep=None, maxsplit=-1) -> list of strings
如果 sep 未指定,则将以空格为分隔符,并在返回的结果中,移除空格;
>>> ' hello world'.split() ['hello', 'world']
等价于:
>>> ' hello world'.strip().split() ['hello', 'world']
注意 split() vs split(’ ')(虽然都是以空格为分割符,无参的 split() 还多了一个移除结果中的空格的动作)
>>> ' hello world'.split(' ') ['', '', '', '', 'hello', '', '', '', '', '', '', '', 'world']
replace(old, new):
>> s = 'hello world hello China' >> s.replace(' ', '|') 'hello|world|hello|China'
str 的静态方法:str.maketrans()
str.maketrans('cs', 'tz')# c ⇒ t# s ⇒ z
translate()
translate 方法和 replace 方法一样,可以替换字符串中的某些部分,但和前者不同的是,translate 方法只处理单个字符,它的优势在于同时进行多个替换,这种情况下会比 replace 的效率高很多。
table = ''.join(map(chr, [97+i for i in range(26)])) table.translate(table, str.maketrans('cs', 'tz'))
join
join 函数的参数为可迭代对象:
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the iterable. The separator between elements is S.
S表示元素之间的分割符,三个元素需要插入两个分割符:
>> l = ['1', '2', '3'] >> '/'.join(l) '1/2/3'
2. join/split
- s.join():表达的是,使用 s 进行中间元素的连接;
>>> s = 'This is \na\ttest'>>> s
'This is \na\t test.'>>> print(s)
This is
a test.>>> s.split()
['This', 'is', 'a', 'test.']>>> ''.join(s.split())
'Thisisatest.'>>> ' '.join(s.split())
'This is a test.'>>> '1'.join(s.split())
'This1is1a1test.'# 只在 list 的元素内部进行添加# '1'.join(['a', 'b', 'c'])# ['a', 'b', 'c'] == > a1b1c# 所以叫 join 嘛
Python 基础 —— str相关推荐
- Python 基础数据类型 -字符串(str)的详细用法
字符串是编程中最重要的数据类型,也是最常见的 1.字符串的表示方式 -单引号' ' 双引号 " " 多引号 """ """ ...
- 字符串从右截取_跟运维组学Python基础day04(字符串str的索引和切片)
内容回顾 跟运维组学Python基础 day03 格式化输出 %s name = input('Pleases input your name: ') # Zanaoprint('My name is ...
- python中用str乘方_Python基础知识
Python2与Python3的区别: (这里只列举自己总结的几点) Python2: 源码不标准 混乱 重复代码过多 默认使用的编码方式是ASCII码 # 解决对应编码问题 在首行增加 #-*-e ...
- Python基础day08【面向对象(类、对象、属性)、魔方方法(init、str、del、repr)】
视频.源码.课件.软件.笔记:超全面Python基础入门教程[十天课程]博客笔记汇总表[黑马程序员] 目录 0.复习 1.类外部添加和获取对象属性 2.类内部操作属性 3.魔法方法 3.1.__i ...
- 一木.溪桥学Python-04: Python基础数据类型int ,float ,bool ,complex,None,str, 转义字符
一木.溪桥 在Logic Education跟Amy学Python 12期:Python基础课 一木.溪桥学Python-04: Python基础数据类型int ,float ,bool ,compl ...
- python基础-变量,变量类型,字符串str,元组tuple,列表list,字典dict操作详解(超详细)
python基础--变量 (文章较长,若需要单独看某一个点,可点击目录直接跳转) 文章目录 python基础--变量 1. 变量 2. 变量类型 2.1数字类型 2.2 字符串 2.3 列表 2.4 ...
- python 等号报错_Python学习----Python基础
Python基础 一.数据类型和变量 1.在Python中,等号=是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量. 例如: a =520# a是整数prin ...
- Python学习笔记1 Python基础
第1章 Python基础 1.1 Python概述及版本介绍 1.Python是一种面向对象的解释型计算机程序设计语言(解释型语言源代码->解释器逐行解释每一句源代码) 2.优点:高级语言.可移 ...
- python基础04
python基础04 python2在编译安装时,可以通过参数 -----enable----unicode=ucs2 或 -----enable--unicode=ucs4 分别用于指定使用2个字节 ...
最新文章
- 机器学习的数学基础 - 特征分解与奇异值分解
- RxJava/RxAndroid:timer(long delay, TimeUnit unit)
- pythonsuper用法_python super函数使用方法详解
- CodeForces - 1579G Minimal Coverage(dp)
- echarts symbol 回调函数_【OpenLayer 实战】实现仿Echarts风格的动态迁徙图/航班图
- mysql innodb禁用事务_MySQL InnoDB事务中锁问题(三)
- 需要使用Git branch switch 的情况Git stash的使用时机
- python必备入门代码-python基础入门这一篇就够
- linux命令高亮显示,colordiff:Linux下高亮显示 diff 输出
- 9447 CTF:no-strings-attached
- FM1208CPU卡读写函数说明
- 4、智慧变电站 - 外围电塔及电线绘制
- 天地图2021版正式发布,清晰程度非常了得
- VS2017+QT5.6.0运行时错误QTableWidget: cannot insert an item that is already owned by another QTableWidget
- 微信公众号二维码生成
- 查找SCI期刊的JCR缩写
- Qt下MQTT模块的导入(源码直接导入)适用Windows和Linux系统
- 网络式数据库和关系式数据库三种
- Linux下开源打包工具fpm的安装与使用(超详细)
- CEO,CTO,COO,CFO,CIO首席执行官,首席运营官,首席技术官
热门文章
- 宏图之下服务器维护,《鸿图之下》3月24日维护更新预告
- python导入xlsx转为csv_Python 拆分工作表为单个工作簿
- 「一本通 6.5 练习 3」迷路
- 吃完饭打嗝原因及治疗方法(分享)
- HiveQL学习笔记(二):Hive基础语法与常用函数
- 数据挖掘项目:银行信用评分卡建模分析(上篇)
- jQuery中的attr()与prop()设置属性、获取属性的区别
- TCP/IP笔记 三.运输层(3)——TCP超时重传算法
- [LeetCode] 81. Search in Rotated Sorted Array II
- 关于 数据库 my_slq的 安装及其卸载