Python:字符串处理函数
split() / join() 拆分和组合
#split() 通过指定分隔符对字符串进行切片(拆分),默认空格符
lan = "python ruby c c++ swift"
lan.split()
#['python', 'ruby', 'c', 'c++', 'swift'] #传入符号','
todos = "download python, install, download ide, learn"
todos.split(', ')
#['download python', 'install', 'download ide', 'learn'] #把它们组合成字符串
','.join(['download python', 'install', 'download ide', 'learn'])
#'download python,install,download ide,learn'
replace() 替换
#replace() 第三个参数为代替几个
s = 'I like C. I like C++. I like Python'
s.replace('like', 'hate')
#'I hate C. I hate C++. I hate Python' s.replace('like', 'hate', 1)
#'I hate C. I like C++. I like Python'
布局center() : 前后留空 ljust():后面留空 rjust():前面留空
align = 'Learn how to align'
align.center(30)
#' Learn how to align ' align.ljust(30)
#'Learn how to align ' align.rjust(30)
#' Learn how to align' ralign = align.rjust(30)
ralign.strip()
#'Learn how to align'
eval(): 将字符串处理为可以运算的类型
# eval() str1 = '1+2'
print(str1) #输出1+2 print(eval(str1)) #输出3
format() :格式化字符串 (New style in Python 3.6)
format(输出项,[,格式化字符串])
格式化输出 1)利用字符串格式化运算符%。 格式化字符串%(输出项1,输出项2....)
%% 百分号
%s 字符串
%c 字符
%d 带符号整数 (10进制)
%o 带符号整数 (8进制)
%x或者%X 带符号整数(16进制)
%e或者%E 浮点数字(科学计数法)
%f或者%F 浮点数字(带小数点)
%g或者%G 根据大小来选择
%e %f %*.*f %(项1,项2,输出项) 例如 %*.*f%(6,2,3.145) 等同 %6.2f%3.145
#格式化字符串的函数 str.format(),它增强了字符串格式化的功能。基本语法是通过 {} 和 : 来代替以前的 %
print('%s %s' % ('one', 'two'))
print('{} {}'.format('one', 'two'))
print('%d %d' % (1, 2))
print('{} {}'.format(1, 2))
#one two
#one two
#1 2
#1 2
print('{1} {0}'.format('one', 'two'))
#two one
a = 5
b = 10
print(f'Five plus ten is {a + b} and not {2 * (a + b)}.')
#Five plus ten is 15 and not 30.
'''
格式字符串还可以指定填充字符、对齐方式(其中<表示左对齐,>表示右对齐、^表示居中、==代表填充字符位于 符号和数字之间)、符号(其中+表示正号,-表示负号)
'''
>>>format(65,"c")
A
>>>print(format(3.145,"6.2f"))
3.15
>>>print(format(3.145,"-6.2f"))
3.15
>>>print(format(3.145,"+6.2f"))
+3.15
>>>print(format(3.145,"<6.2f"))
3.15
>>>print(format(3.145,">6.2f"))
3.15
>>>print(format(3.145,"><6.2f"))
3.15>>
>>>print(format(3.145,"<=6.2f"))
<<3.15
>>>print(format(3.145,"=6.2f"))
3.15
>>>print(format(3.145,"^6.2f"))
3.15
>><print(format(3.145,"0=+10"))
+00003.145
其他
#以下函数都是返回新的值,不影响原来的字符串
kk = "Python description: Python is a programming language that lets you work quickly and integrate systems more effectively." kk.startswith('Python')
#True kk.endswith('effectively.')
#True kk.find('language')
# #检测字符串是否由字母和数字组成
kk.isalnum()
#False kk.count("Python")
# #移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
kk.strip('.')
#'Python description: Python is a programming language that lets you work quickly and integrate systems more effectively' kk.upper()
#'PYTHON DESCRIPTION: PYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.' kk.lower()
#'python description: python is a programming language that lets you work quickly and integrate systems more effectively.' kk.title()
#每个单词开头都大写
#'Python Description: Python Is A Programming Language That Lets You Work Quickly And Integrate Systems More Effectively.' kk.capitalize()
#首个单词开头大写,后面不大写
#Python description: python is a programming language that lets you work quickly and integrate systems more effectively. kk.swapcase()
#每个字母大小写调换
#pYTHON DESCRIPTION: pYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.
Python:字符串处理函数的更多相关文章
- Python—字符串常用函数
Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- Python 字符串常用函数
操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...
- Python 字符串操作函数一
#-*- coding:utf-8 -*- strword = "i will fly with you , fly on the sky ." #find print(strwo ...
- python——字符串操作函数
字符串 join() map() split() rsplit() splitlines() partiton() rpartition() upper() lower() swapcase() ca ...
- python字符串常用函数
# 索引与切片 *** capitalize() **首字母大写 upper() lower() *** 大写和小写函数 startswith endswith *** 判断以‘’字母’开 ...
- Python 字符串操作函数二
#-*- coding:utf-8 -*- line = "l want watch movie with you ." print(line.center(50)) print( ...
- Python - 字符串常用函数详解
str.index(sub, start=None, end=None) 作用:查看sub是否在字符串中,在的话返回索引,且只返回第一次匹配到的索引:若找不到则报错:可以指定统计的范围,[start, ...
- python字符串常用函数-大小写,删除空格,字符串切片
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
随机推荐
- iOS UTI(统一类型标识)
同一类型标识符(Uniform Type Identifier,UTI)代表IOS信息共享的中心组件.可以把它看成下一代的MIME类型.UTI是标识资源类型(比如图像和文本)的字符串,他们制定哪些类型 ...
- 【GIS】地球经纬度和米换算(转)
经度的定义是过某点的经线面和本初子午面之间的夹角.纬度的定义是过某点的球面切面垂线与赤道平面之间的线面角.可见,如果不加限定,1"之间的距离没有意义. 假设地球为一半径为R的表面光滑圆球体, ...
- mysql的联表删除
联表删除: 1.从数据表t1 中把那些id值在数据表t2 里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或DELETE FROM t1 ...
- Elasticsearch未授权访问漏洞
Elasticsearch服务普遍存在一个未授权访问的问题,攻击者通常可以请求一个开放9200或9300的服务器进行恶意攻击. 0x00 Elasticsearch 安装 前提,保证安装了JDK 1. ...
- 使用 urllib 分析 Robots 协议
(1) Robots Exclusion Protocol,网络爬虫排除标准,也称爬虫协议.机器人协议,用来告诉爬虫和搜索引擎哪些页面可以抓取,哪些不可以抓取(2) Robots 协议以 robots ...
- <转>python字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- 【Spring源码深度解析学习系列】Bean的加载(六)
Bean的加载所涉及到的大致步骤: 1)转换对应beanName 为什么需要转换beanName呢?因为传入的参数可能是别名,也可能是FactoryBean,所以需要一系列的解析,这些解析内容包括如下 ...
- 国内首款 FPGA 云服务器,性能是通用 CPU 服务器 30 倍以上
版权声明:本文由薛梁原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/628340001485134638 来源:腾云阁 ht ...
- Android org.apache.http.*找不到
https://blog.csdn.net/u012005313/article/details/51499892 直接把 org.apache.http.legacy.jar 报拷贝出来,放到Ecl ...
- Struts2之命名空间与Action的三种创建方式
看到上面的标题,相信大家已经知道我们接下来要探讨的知识了,一共两点:1.package命名空间设置:2.三种Action的创建方式.下面我们开始本篇的内容: 首先我们聊一聊命名空间的知识,namesp ...