python字符串的方法
python字符串的方法
############7个基本方法############
1:join
def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
"""
Concatenate any number of strings. The string whose method is called is inserted in between each given string.
The result is returned as a new string. Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
"""
可以向字符串插入 标识符 标点 字母等连接
test=("qweqweqw")
v="_".join(test)
print(v)
q_w_e_q_w_e_q_w
test=(['as','df','as'])
v="_".join(test)
print(v)
as_df_as
2:split(正则)
split(self, *args, **kwargs): # real signature unknown
"""
Return a list of the words in the string, using sep as the delimiter string. sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
"""
全分割 从第一个逐一扫描字符串寻找所选字符 从字符串中找到该字符位置开始分割 (所选字符消失)
test=("asdfasdfasdfsadfsdf")
v=test.split("f")
print(v)
['asd', 'asd', 'asd', 'sad', 'sd', '']
test=("asdfasdfasdf")
v=test.split("s",2,)
print(v)
['a', 'dfa', 'dfasdf']
3:find
find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation. Return -1 on failure.
"""
return 0
从左向右查找所定义的字符 显示首个该字符的索引(下标);find(字符,开始索引,结束索引)
test=("asdfasdfasdfsadfsdf")
v=test.find("f")
print(v)
3
test=("asdfasdfasdfsadfsdf")
v=test.find("f",5,15)
print(v)
7
4:strip
strip(self, *args, **kwargs): # real signature unknown
"""
Return a copy of the string with leading and trailing whitespace remove. If chars is given and not None, remove characters in chars instead.
"""
清除字符串两边的空格
test=(" asdfasdfas dfsadfsdf ")
v=test.strip()
print(v)
asdfasdfas dfsadfsdf
5:upper
upper(self, *args, **kwargs): # real signature unknown
""" Return a copy of the string converted to uppercase. """
字符串全大写
test=("asdfasdfasdfsadfsdf")
v=test.upper()
print(v)
ASDFASDFASDFSADFSDF
6:lower
lower(self, *args, **kwargs): # real signature unknown
""" Return a copy of the string converted to lowercase. """
pass
字符串全小写
test=("ASDFASDFASDFSADFSDF")
v=test.lower()
print(v)
asdfasdfasdfsadfsdf
7:replace
replace(self, *args, **kwargs): # real signature unknown
"""
Return a copy with all occurrences of substring old replaced by new. count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences. If the optional argument count is given, only the first count occurrences are
replaced.
"""
替换所选字符 replace(“文本内的字符”,“想要替换文本的字符”,“替换的数量”)
test=("ASDFASDFASDFSADFSDF")
v=test.replace("ASD","aaa",1)
print(v)
aaaFASDFASDFSADFSDF
test=("ASDFASDFASDFSADFSDF")
v=test.replace("ASD","aaa")
print(v)
aaaFaaaFaaaFSADFSDF
python字符串的方法的更多相关文章
- python字符串replace()方法
python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...) S.repla ...
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
- 7. python 字符串格式化方法(1)
7. python 字符串格式化方法(1) 承接上一章节,我们这一节来说说字符串格式化的另一种方法,就是调用format() >>> template='{0},{1} and {2 ...
- python字符串格式化方法 format函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...
- Python字符串解析方法汇总
Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: ...
- python字符串排序方法
一般情况下,python中对一个字符串排序相当麻烦: 一.python中的字符串类型是不允许直接改变元素的.必须先把要排序的字符串放在容器里,如list. 二.python中的list容器的sort( ...
- python字符串处理方法
一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...
- python字符串连接方法效率比较
方法1:直接通过加号(+)操作符连接 1 website = 'python' + 'tab' + '.com' 方法2:join方法 1 2 listStr = ['python', 'tab', ...
- 【Python基础教程】三种常用、效率最高的Python字符串拼接方法
python字符串连接的方法,一般有以下三种: **方法1:**直接通过加号(+)操作符连接website=& 39;python& 39;+& 39;tab& 39; ...
随机推荐
- Arch Linux 软件包的查询及清理
包的查询及清理 列出所有本地软件包(-Q,query查询本地:-q省略版本号) $ pacman -Qq (列出有816个包) 列出所有显式安装(-e,explicitly显式安装:-n忽略外部包AU ...
- canvas绘图——根据鼠标位置进行缩放的实现原理
以任一点 A 缩放的基本原理: A 点为鼠标位置,正常放大是以原点放大,那么放大后 A 点就会变到 A1 点的位置 x1, y1.然后再将画布进行位移,将 A1 的位置移动到 A,则完成以 A 点缩放 ...
- gtest 三种事件机制
前言: 1.首先说明gtest中事件的结构层次: 测试程序:一个测试程序只有一个main函数,也可以说是一个可执行程序是一个测试程序.该级别的事件机制会在程序的开始和结束执行. 测试套件:代表一个测试 ...
- 转:Excel—“撤销工作表保护密码”的破解并获取原始密码
在日常工作中,您是否遇到过这样的情况:您用Excel编制的报表.表格.程序等,在单元格中设置了公式.函数等,为了防止其他人修改您的设置或者防止您自己无意中修改,您可能会使用Excel的工作表保护功能, ...
- 温度转换 II
描述 温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit). 请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度. 转换算法如下:(C表示摄氏度.F表示 ...
- AE插件:能量激光描边光效特效Saber Mac汉化版
与大家分享一款非常好用的AE插件Saber插件汉化版.videocopilot saber是一款能量激光描边光效特效AE插件,可以帮助用户制作出能量激光.传送门.霓虹灯.电流.光束.光剑等效果.小编现 ...
- 动画讲解TCP的3次握手,4次挥手
https://mp.weixin.qq.com/s/TUBhH_lJe6M4KgAZO-rP2A TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入 ...
- JWT(JSON Web Token) 【转载】
JWT(JSON Web Token) 什么叫JWTJSON Web Token(JWT)是目前最流行的跨域身份验证解决方案. 一般来说,互联网用户认证是这样子的. 1.用户向服务器发送用户名和密码. ...
- 左耳听风-ARTS-第3周(2019/4/7-2019/4/13)
Algorithm 本周的算法题是按顺序合并两个已排序的链表(https://leetcode.com/problems/merge-two-sorted-lists/).和归并排序的合并已排序数组的 ...
- cpu工作原理