首页 > 编程语言 >python之字符串二

python之字符串二

时间:2024-01-19 16:24:49浏览次数:31  
标签:string python men print str 字符串

字符串详解                                     

1.  index

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(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.

Raises ValueError when the substring is not found.
"""
return 0
翻译:格式s.index(sub,开始位置,结束位置)--》返回整型
返回sub字符所在位置的最小位置
并且sub所在【开始:结束】范围内,可选参数开始位置和结束位置解释为切片表示法
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.index('i',4,9))
View Code

2.isdigit

def isdigit(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is a digit string, False otherwise.

A string is a digit string if all characters in the string are digits and there
is at least one character in the string.
"""
pass
翻译:如果字符串是数字字符串则返回True,否则返回False
如果字符串里面的所有字符都是数字,则是个数字字符串,并且这个字符串不为空字符串。
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men 123456'
4 print(str.isdigit())#是否数字
View Code

3.isalnum

def isalnum(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is an alpha-numeric string, False otherwise.

A string is alpha-numeric if all characters in the string are alpha-numeric and
there is at least one character in the string.
"""
pass
翻译:如果字符串是字母数字的则返回True,否则返回False
如果字符串里面只包含字母和数字,则是字母数字字符串,并且这个字符串不为空字符串。
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men 123456'
4 print(str.isalpha())
View Code

4.isalpha

def isalpha(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is an alphabetic string, False otherwise.

A string is alphabetic if all characters in the string are alphabetic and there
is at least one character in the string.
"""
pass
翻译:如果字符串是字母,则返回True,否则返回False
如果字符串里面只包含字母,则是字母字符串,并且这个字符串不为空字符串
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men 123456'
4 print(str.isalpha())
View Code

5.isascii

def isascii(self, *args, **kwargs): # real signature unknown
"""
Return True if all characters in the string are ASCII, False otherwise.

ASCII characters have code points in the range U+0000-U+007F.
Empty string is ASCII too.
"""
pass
翻译:如果字符串是ASCII则返回True,否则返回False
ASCII字符串是码值范围在U+0000-U+007F间,空字符串也是ASCII
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men 123456'
4 print(str.isascii())
View Code

6.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'
"""
pass
翻译:将任意字符的字符串连接起来
被调用方法的字符串被插入每个给的字符串中间,结果返回一个新的字符串
比如'.'.join('abc')-->'a.b.c'在a与b之间插入.以及在b与c之间插入.
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 str1='abcd'
5 print(str.join(str1))
View Code

7.ljust/rjust

def ljust(self, *args, **kwargs): # real signature unknown
"""
Return a left-justified/right-justified string of length width.
    
Padding is done using the specified fill character (default is a space).
"""
pass
翻译:返回长度为width的左对齐字符串
用指定字符填充(默认为空格)
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.ljust(30,'-'))
5 print(str.rjust(30,'-'))
View Code

8.partition

def partition(self, *args, **kwargs): # real signature unknown
"""
Partition the string into three parts using the given separator.

This will search for the separator in the string. If the separator is found,
returns a 3-tuple containing the part before the separator, the separator
itself, and the part after it.

If the separator is not found, returns a 3-tuple containing the original string
and two empty strings.
"""
pass

翻译:用给的分隔符将字符串分成3部分;
这将要求在字符串中找分割符,如果分隔符找到,则返回3个源组包含分隔符的部分(分隔符前面部分,分隔符和分隔符后面的部分)
如果分隔符没有被找到,则返回一个原始字符串及2个空字符串
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.partition('i'))
View Code

9.removeprefix

def removeprefix(self, *args, **kwargs): # real signature unknown
"""
Return a str with the given prefix string removed if present.

If the string starts with the prefix string, return string[len(prefix):].
Otherwise, return a copy of the original string.
"""
pass
翻译:如果给定的前缀存在,则返回一个删除该字符的字符串
如果字符串是以给定的字符开头,则返回字符串[len(prefix):],否则返回原始字符串
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.removeprefix('wo'))
View Code

10.removesuffix

def removesuffix(self, *args, **kwargs): # real signature unknown
"""
Return a str with the given suffix string removed if present.

If the string ends with the suffix string and that suffix is not empty,
return string[:-len(suffix)]. Otherwise, return a copy of the original
string.
"""
pass
翻译:如果是以给定字符结尾的则返回删除这个字符的字符串
如果字符串是以给定的字符结尾的,则返回删除该字符的字符串,否则返回原始字符串
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.removesuffix('n'))
View Code

11.strip

def strip(self, *args, **kwargs): # real signature unknown
"""
Return a copy of the string with leading and trailing whitespace removed.

If chars is given and not None, remove characters in chars instead.
"""
pass
翻译:返回一个删除开头和结尾为空的字符串
如果给定字符存在,则删除字符串中给定字符
1 #!/usr/bin/python
2 #字符串
3 str=' wo ai beii Jing Tian an men '
4 print(str.strip())
View Code

12.split

def 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.
"""
pass
翻译:使用sep最为分隔字符串的,返回由字符串中字符组成的列表
分隔符是分隔字符串的,默认以空格进行分隔,并且在结果中丢弃空字符。
最大分隔,指分隔的最大数,-1(默认值)为没有限制
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men'
4 print(str.split())
View Code

13.splitlines

def splitlines(self, *args, **kwargs): # real signature unknown
"""
Return a list of the lines in the string, breaking at line boundaries.

Line breaks are not included in the resulting list unless keepends is given and
true.
"""
pass
翻译:以行为边界,返回字符串中一行的列表
结果不会包含分隔符,除非keepends给定为True
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men\ntian an men tai yang sheng'
4 print(str.splitlines(keepends=False)) #keepends是否保留分隔符(\r \r\n \n) \r--换行 \n--回车
View Code

14.swapcase

def swapcase(self, *args, **kwargs): # real signature unknown
""" Convert uppercase characters to lowercase and lowercase characters to uppercase. """
pass
翻译:大小写转换
1 #!/usr/bin/python
2 #字符串
3 str='wo ai beii Jing Tian an men\ntian an men tai yang sheng'
4 print(str.swapcase())
View Code

15.translate

def translate(self, *args, **kwargs): # real signature unknown
"""
Replace each character in the string using the given translation table.

table
Translation table, which must be a mapping of Unicode ordinals to
Unicode ordinals, strings, or None.

The table must implement lookup/indexing via __getitem__, for instance a
dictionary or list. If this operation raises LookupError, the character is
left untouched. Characters mapped to None are deleted.
"""
pass
翻译:用给的表单替换字符串的字符
表单:翻译的表单必须是unicode序列的字典映射
表单必须是通过__getitem__查找,比如说字典或列表,如果这种操作导致lookup错误,那么字符没有影响,映射到none的字符将被删除
1 str='wo ai beii Jing Tian an men\ntian an men tai yang sheng'
2 intab ="aeiou"
3 outtab='12345'
4 trantab=str.maketrans(intab,outtab)
5 print(trantab)
6 print(str.translate(trantab))
View Code                                                                        

标签:string,python,men,print,str,字符串
From: https://www.cnblogs.com/Little-Girl/p/17974938

相关文章

  • 洛谷题单指南-模拟和高精度-P1098 [NOIP2007 提高组] 字符串的展开
    原题链接:https://www.luogu.com.cn/problem/P1098题意解读:题目本身是一道模拟题,但是细节点较多,要拿100分,有以下注意点:1、-号两个需要同时为小写字母或者数字,才进行填充2、-号左边>=右边,直接输出-3、对待填充的内容的处理,可以先看是否填充*;小写字母和数字的填充都是前一位asci......
  • Python实现PowerPoint(PPT/PPTX)到PDF的批量转换
    如果需要处理大量的PPT转PDF的工作,一个个打开并另存为PDF是非常费时的做法。我们可以利用Python编程语言的强大的工具来自动化这个过程,使得批量转换变得简单而高效。本文将介绍如何使用Python将PowerPoint演示文稿(PPT、PPTX等)转换为PDF文件,使演示内容能够在更多的设备上展示,且保持......
  • Jmeter、postman、python 三大主流技术如何操作数据库?
    1、前言只要是做测试工作的,必然会接触到数据库,数据库在工作中的主要应用场景包括但不限于以下:功能测试中,涉及数据展示功能,需查库校验数据正确及完整性;例如商品搜索功能自动化测试或性能测试中,某些接口要跑通,需要关联到数据库操作;例如注册接口中短信验证码获取自动化测试中......
  • 使用Python监听并下载微信聊天表情包
    实现的功能只要有人给你发了表情包,不管是群聊还是个人发的,都将它保存到本地。也许某天斗图的时候就能用到,不过即使有了表情包,还需要一个检索功能,不然这一张一张看也太费眼睛了。检索表情包检索表情包的功能实现比较麻烦,至少需要两个模型:ocr和图片描述生成,如果更复杂点的话还需......
  • 慎用!3个容易被打的Python恶搞脚本
    Python无限恶搞朋友电脑,别提有多爽了,哈哈,打造自己的壁纸修改器,电脑无限锁屏,无线弹窗,都在这里!!!1、修改电脑桌面壁纸工具使用开发环境:python3.7, Windows10使用工具包:win32api,win32con,win32gui,os,randomwin32的工具下载命令:pipinstallpywin32项目解析思路桌面数据信息是保存......
  • python第三节:Str字符串类型(8)
    str.strip([chars])返回原字符串的副本,移除其中的前导和末尾字符。 chars 参数为指定要移除字符的字符串。如果省略或为None,则 chars 参数默认移除空白符。注意:参数chars 并非作为一个整体,而是会移除参数值的所有组合。在移除字符串前后的指定字符时,遇到不在参数组合中的字......
  • Python将JSON以表格数据格式导出
      本文介绍基于Python语言,读取JSON格式的数据,提取其中的指定内容,并将提取到的数据保存到.csv格式或.xlsx格式的表格文件中的方法。  JSON格式的数据在数据信息交换过程中经常使用,但是相对而言并不直观;因此,有时我们希望将JSON格式的数据转换为Excel表格文件数据;这里就介绍一下......
  • 对象数组,根据字符串字段,并按默认方式排序
    sort在字符串的默认排序,是按unicode字节码排序的,一般字符串的排序可以通过strA.localeCompare(strB)来完成,但我这里必须要按字符串的默认方式排序。list=list.sort((a,b)=>{varjobA=a.Job;varjobB=b.Job;......
  • oracle查找字符串中指定字符的位置
    INSTR(string,search_string,start_position,nth_appearance)其中:string是要查找的字符串。search_string是要查找的字符或字符串。start_position是开始查找的位置,默认为1。nth_appearance是要查找的字符或字符串在string中出现的次数,可以省略,默认为1......
  • python 有效的数独 多种解法
    解法一:暴力枚举法最简单的方法是对于每一行、每一列和每一个3x3的九宫格,分别判断其中是否有重复的数字。具体实现如下:classSolution:defisValidSudoku(self,board:List[List[str]])->bool:#检查行foriinrange(9):nums=set()......