iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python字符串String模块
  • 927
分享到

python字符串String模块

字符串模块python 2023-01-31 02:01:27 927人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

python的string模块1.字符串属性方法操作:1.>字符串格式输出对齐>>> str = "Python stRING"  >>> print str.center(20)        

python的string模块


1.字符串属性方法操作:


1.>字符串格式输出对齐

>>> str = "Python stRING" 

 

>>> print str.center(20)           #生成20个字符长度,str排中间

   Python stRING   

    

>>> print str.ljust(20)            #生成20个字符长度,str左对齐

Python stRING      

 

>>> print str.rjust(20)            #生成20个字符长度,str右对齐

       Python stRING


2.>大小写转换

>>> str = "Python stRING"

 

>>> str.upper()                #转大写

'PYTHON STRING'

 

>>> str.lower()                #转小写 

'python string'

 

>>> str.capitalize()           #字符串首为大写,其余小写

'Python string'

 

>>> str.swapcase()              #大小写对换 

'pYTHON STring'

 

>>> str.title()                #以分隔符为标记,首字符为大写,其余为小写

'Python String'


3.>字符串条件判断


>>> str = '01234'

 

>>> str.isalnum()                #是否全是字母和数字,并至少有一个字符

True

>>> str.isdigit()                #是否全是数字,并至少有一个字符

True      


>>> str = 'string'

 

>>> str.isalnum()                  #是否全是字母和数字,并至少有一个字符

True

>>> str.isalpha()                  #是否全是字母,并至少有一个字符 

True

>>> str.islower()                  #是否全是小写,当全是小写和数字一起时候,也判断为True

True

 

>>> str = "01234abcd"

 

>>> str.islower()                  #是否全是小写,当全是小写和数字一起时候,也判断为True

True

 

>>> str.isalnum()                  #是否全是字母和数字,并至少有一个字符

True

 

>>> str = ' '

>>> str.isspace()                  #是否全是空白字符,并至少有一个字符

True

 

>>> str = 'ABC'

 

>>> str.isupper()                  #是否全是大写,当全是大写和数字一起时候,也判断为True

True

 

>>> str = 'Aaa Bbb'

 

>>> str.istitle()                  #所有单词字首都是大写,标题 

True

 

 

>>> str = 'string learn'

 

>>> str.startswith('str')                 #判断字符串以'str'开头

True

 

>>> str.endswith('arn')                      #判读字符串以'arn'结尾

True


4.>字符串搜索定位与替换


>>> str='string lEARn'

 

>>> str.find('z')              #查找字符串,没有则返回-1,有则返回查到到第一个匹配的索引

-1

 

>>> str.find('n')              #返回查到到第一个匹配的索引

4

 

>>> str.rfind('n')         #返回的索引是最后一次匹配的

11

 

>>> str.index('a')         #如果没有匹配则报错 


Traceback (most recent call last):

  File "<input>", line 1, in <module>

ValueError: substring not found

  

>>> str.index("n")      #同find类似,返回第一次匹配的索引值

4

 

>>> str.rindex("n")         #返回最后一次匹配的索引值

11

 

>>> str.count('a')      #字符串中匹配的次数

0

>>> str.count('n')      #同上

2

 

>>> str.replace('EAR','ear')        #匹配替换

'string learn'

 

>>> str.replace('n','N')

'striNg lEARN'

 

>>> str.replace('n','N',1)

'striNg lEARn'

 

>>> str.strip('n')          #删除字符串首尾匹配的字符,通常用于默认删除回车符 

   

'string lEAR' 

   

>>> str.lstrip('n')        #左匹配 

   

'string lEARn' 

   

>>> str.rstrip('n')        #右匹配 

   

'string lEAR' 

 

>>> str = " tab"

 

>>> str.expandtabs()       #把制表符转为空格

' tab'

 

>>> str.expandtabs(2)      #指定空格数

' tab'


5.>字符串编码与解码


>>> str = "字符串学习"

>>> str

'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'

 

>>> str.decode('utf-8')                                #解码过程,将utf-8解码为unicode

u'\u5b57\u7b26\u4e32\u5b66\u4e60'

 

>>> str.decode("utf-8").encode('gbk')                      #编码过程,将unicode编码为gbk

'\xd7\xd6\xb7\xfb\xb4\xae\xd1\xa7\xcf\xb0'

 

>>> str.decode('utf-8').encode('utf-8')                        #将unicode编码为utf-8 

'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'


6.>字符串分割变换


>> str = "Learn string"

 

>>> '-'.join(str)


'L-e-a-r-n- -s-t-r-i-n-g'

 

>>> li = ['Learn','string']

 

>>> '-'.join(li)


'Learn-string'

 

>>> str.split('n')

['Lear', ' stri', 'g']

 

>>> str.split('n',1)

['Lear', ' string']

 

>>> str.rsplit('n')

['Lear', ' stri', 'g']

 

>>> str.rsplit('n',1)

['Learn stri', 'g']

 

>>> str.splitlines()

['Learn string']

 

>>> str.partition('n')

('Lear', 'n', ' string')

 

>>> str.rpartition('n')

('Learn stri', 'n', 'g')




  1.    string.atof(s) 字符串转换成浮点型


string.atof('1.11')

输出结果:1.11

string.atof('1')

输出结果:1.0

 

2.     string.atoi(s[, base]) 字符串转换成整型


string.atoi('11') or string.atoi('11', 10)

输出结果:11

string.atoi('11', 2)

输出结果:3

string.atoi('11', 8)

输出结果:9

string.atoi('11', 16)

输出结果:17

 

3.     string.capitalize(s) 字符串的第一个字符转换成大写


string.capitalize('hello world')

输出结果:Hello world

 

4.     string.capwords(s[, sep]) 字符串以sep为分隔符分割后的每个字段的首位转换为大写


string.capWords('hello world')

输出结果:Hello World

string.capwords('hello world', 'l')

输出结果:HellO worlD

 

5.     string.center(s, len[, fillchar])字符串转换成指定长度,不够的用fillchar补充,且补充的字符在两边

string.center('hello world', 10, '*')

输出结果:hello world

string.center('hello world', 15, '*')

输出结果:**hello world**

 

6.     string.count(s, sub[, start[, end]])查询sub在s中的个数

string.count('hello world', 'l')

输出结果:3

string.count('hello world', 'l', 3)

输出结果:2

string.count('hello world', 'l', 3, 6)

输出结果:1

7.     string.find(s, sub[, start,[end]]) 查询sub在s中的第一个位置

string.find('hello world', 'l')

输出结果:2

string.find('hello world', 'l', 4, 6)

输出结果:-1

 

8.     string.ljust(s, len[, fillchar])字符串左对齐,不够用fillchar补充

string.ljust('hello world', 15)

输出结果:hello world

string.ljust('hello world', 15, '*')

输出结果:hello world****

 

9.     string.lstrip(s[, chars]) 清除左边的空白字符

string.lstrip(' hello world')

输出结果:hello world

string.lstrip('hello world', 'h')

输出结果:ello world

 

10.    string.upper(s) 字符串转换成大写的

string.upper('hello world')

输出结果:HELLO WORLD

 

11.    string.join(list[, sep]) list里的字符串用sep连接起来

string.join(['hello', 'world'])

输出结果:hello world

string.join(['hello', 'world'], '*')

输出结果:hello*world

 

12.    string.replace(s, old, new[,max]) 字符串s里的old替换为new,最多替换为max次

string.replace('hello world', 'l', 'L')

输出结果:heLLo worLd

string.replace('hello world', 'l', 'L', 1)

输出结果:heLlo world

 

13.    string.translate(s, table[,delchar]) 字符串转换为指定的table里的字符,且删除指定的delchar

table = string.maketrans('hello', 'HELLO')

string.translate('hello world', table)

输出结果:HELLO wOrLd

string.translate('hello world', table, 'l')

输出结果:HEO wOrd

 

14.    string.split(s[, sep[,maxsplit]])  字符串以sep作为分隔符,maxsplit作为分隔次数进行分隔

string.split('hello world')


输出结果:['hello', 'world']


string.split('hello world', 'l')


输出结果:['he', '', 'o wor', 'd']


string.split('hello world', 'l', 1)


输出结果:['he', 'lo world']


模板

map = {'var': 'hello world'}

tmp = string.Template('my first output:${var}')

tmp.substitute(map)

输出结果:my first output: hello world

tmp.safe_substitute(map)

输出结果:同上

 

map = {'var': 'hello world'}

tmp = string.Template('my first output:${vars}')

tmp.substitute(map)

输出结果:

tmp.safe_substitute(map)

输出结果:my first output: ${vars}




1 基本字符串操作

说明:字符串也是序列的一种,所以分片,乘法,索引,求长度,最大, 最小,判断成员资格等都可以应用在字符串上;

注意:字符串是不可变的,所以不能对其进行赋值;

例子

1:  >>> mystr="Test string"


2:  >>> mystr[0] = 't'


3:  Traceback (most recent call last):


4:  File "<pyshell#1>", line 1, in <module>


5:    mystr[0] = 't'


6:  TypeError: 'str' object does not support item assignment


7:  >>>


2 字符串格式化:精简版

 

2.1 用字符串格式化操作符


说明:字符串格式化使用字符串格式化操作符百分号( % )实现,在操作符的左侧是格式化字符串,右侧是希望被格式化的值;

注意:

只有元组和字典可以被格式化为一个以上的值,列表和其他序列会被格式化为一个值;

转换说明符,用于标记需要插入转换值的位置;

如果在格式化字符串中要输出百分号,则需要使用 %%

例子:

 1:  #一般格式化


 2:  >>> myfORMat = "Hello, my name is %s %s"


 3:  >>> name = ('Bill','Gunn')


 4:  >>> print (myformat % name)


 5:  Hello, my name is Bill Gunn


 6:  >>>


 7:  


 8:  #用列表格式化


 9:  >>> myformat = 'Hello, my name is %s'


10:  >>> name=['Bill', 'Gunn']


11:  >>> print(myformat % name)


12:  Hello, my name is ['Bill', 'Gunn']


13:  


14:  #打印浮点数


15:  >>> import math


16:  >>> print ("PI = %.5f" % pi)


17:  PI = 3.14159


18:  


19:  #打印百分号


20:  >>> print("%.2f%%"% 22.3)


21:  22.30%


22:  >>>


2.2 用string的Template格式化字符串


说明:类似于Unix Shell中的变量替换,使用substitute方法,将字符串 模板中的$foo替换为传递进来的参数foo

例子:

 1:  #从string模块中导入Template


 2:  >>> from string import Template


 3:  #创建模板


 4:  >>> myformat = Template("My name is $name")


 5:  #替换变量并打印


 6:  >>> print(myformat.substitute(name="Bill Gunn"))


 7:  My name is Bill Gunn


 8:  >>>


 9:  


10:  #输出美元符号的方法,在模板里输入两个$


11:  >>> mytemplate = Template("The price is $$$price")


12:  >>> mytemplate.substitute(price=100)


13:  'The price is $100'


14:  >>>


15:  


16:  #如果参数与后面的字符串相连,需要用大括号将其括起来


17:  >>> from string import Template


18:  >>> mytemplate = Template("It's ${x}tastic!")


19:  >>> mytemplate.substitute(x='slum')


20:  "It's slumtastic!"


21:  >>>


22:  


23:  #使用字典替换参数


24:  >>> mytemplate = Template("My $property is $value")


25:  >>> name = {}


26:  >>> name["property"] = "name"


27:  >>> name["value"] = "Bill Gunn"


28:  >>> mytemplate.substitute(name)


29:  'My name is Bill Gunn'


30:  >>>


31:  


3 字符串格式化:完整版

说明:字符串格式化操作符的右操作数如果是元组,那么在格式化字符串 中必须将元组中的各个元素都有对应的转义说明符。

例子:

 1:  >>> data = tuple(list("123"))


 2:  >>> data


 3:  ('1', '2', '3')


 4:  #格式化字符串中只有一个转义说明符,而元组中有三个元素,转换会报错


 5:  >>> print ("data is %s" % data)


 6:  Traceback (most recent call last):


 7:    File "<pyshell#18>", line 1, in <module>


 8:      print ("data is %s" % data)


 9:  TypeError: not all arguments converted during string formatting


10:  #显示元组中的全部元素


11:  >>> print ("data is %s %s %s" % data)


12:  data is 1 2 3


13:  >>>


14:  


3.1 转换说明符


转换说明符

 

转义说明符 含义

d,i 带符号的十进制整数

o 不带符号的八进制

u 不带符号的十进制

x 不带符号的十六进制(小写)

X 不带符号的十六进制(大写)

e 科学计数法的浮点数(小写)

E 科学计数法的浮点数(大写)

f,F 十进制浮点数

g 如果指数大于-4或者小于精度值则和e相同,否则和f相同

G 如果指数大于-4或者小于精度值则和E相同,否则和F相同

C 单字符(接受整数或者单字符字符串)

r 字符串(使用repr转换任意Python对象)

s 字符串(使用str转换任意Python对象)

3.2 简单转换


例子:

 1:  #十进制整数


 2:  >>> print ("The price is $%d" % 12)


 3:  The price is $12


 4:  


 5:  #十六进制整数


 6:  >>> print ("Hex %x" % 12)


 7:  Hex c


 8:  


 9:  #八进制整数


10:  >>> print ("Oct %o" % 12)


11:  Oct 14


12:  >>>


13:  


3.3 字段宽度和精度


说明:

字段宽度:转换后的值所保留的最小字符个数;

字段精度:转换后,结果中应该的小数位数;

可以使用*作为字段宽度或者精度

例子:

 1:  #限制宽度


 2:  >>> "%10f" % math.pi


 3:  '  3.141593'


 4:  


 5:  #限制小数位数


 6:  >>> "%5.2f" % math.pi


 7:  ' 3.14'


 8:  


 9:  #用星号限制宽度和精度,下例中,宽度为10,精度为5


10:  >>> '%*.*s' % (10, 5, 'adfasdfadsfasdfasdfasdf')


11:  '     adfas'


12:  >>>


13:  


3.4 符号,对齐和 0 填充


说明:

零:宽度不够时用数字0填充;

负号:左对齐;

正号:不管是正数还是负数都标记出符号

空格:宽度不够时用空格填充;

例子:

 1:  #空白补0


 2:  >>> print ("%010f" % math.pi)


 3:  003.141593


 4:  


 5:  #左对齐


 6:  >>> "%-10.2f" % math.pi


 7:  '3.14      '


 8:  


 9:  #空白右对齐


10:  >>> print("% 5d\n% 5d" % (123, 12))


11:    123


12:     12


13:  


14:  #显示正负符号


15:  >>> print ("%+5d\n%+5d" % (123, -123))


16:   +123


17:   -123


18:  >>>


19:  


4 字符串方法

 

4.1 find


说明:用于在长字符串中查找子字符串,如果找到,则返回子字符串在左 侧第一次出现的索引,没找到返回-1,在查找时,还可以指定在长字符串 中查找的范围,提供起始索引和结束索引作为查找的参数;

注意:查找时,包括起始索引位置,但是不包括结束索引的位置;

例子:

 1:  >>> string.ascii_letters


 2:  'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'


 3:  >>> letters = string.ascii_letters


 4:  >>> letters.find('AB')


 5:  26


 6:  >>> letters.find('X',30,-1)


 7:  49


 8:  >>> letters.find("AB",26)


 9:  26


10:  


4.2 join


说明:将队列中的元素用字符串连接起来,并且列表中的元素必须是字符 串;

例子:

1:  >>> data = list('123456')


2:  >>> data


3:  ['1', '2', '3', '4', '5', '6']


4:  >>> "AB".join(data)


5:  '1AB2AB3AB4AB5AB6'


6:  >>> 


7:  


4.3 lower


说明:将字符串转换成小写字母,并返回,但是原字符串不改变

例子:

1:  >>> mystr="ABCD"


2:  >>> mystr.lower()


3:  'abcd'


4:  >>> mystr


5:  'ABCD'


6:  >>> 


7:  


4.4 replace


说明:返回所有匹配项都被替换之后的字符串

例子:

1:  >>> mystr = "My name is Geng Qi"


2:  >>> mystr.replace("Geng Qi", "Bill Gunn")


3:  'My name is Bill Gunn'


4:  >>> 


4.5 split


说明:将字符串分割成序列;

注意:如果不提供分割符,则会将空白符当作分割符

例子

 1:  #以加号为分割符


 2:  >>> mystr = "1+2+3+4+5+6"


 3:  >>> mystr.split('+')


 4:  ['1', '2', '3', '4', '5', '6']


 5:  


 6:  #不提供分割符时,以空白符为分割符


 7:  >>> mystr = "This    is a       test string"


 8:  >>> mystr.split()


 9:  ['This', 'is', 'a', 'test', 'string']


10:  >>> 


11:  


4.6 strip


说明:去除两侧的空白,也可以去除指定的字符

例子:

 1:  >>> mystr = "           asdfad adfasf asdf                      "


 2:  >>> mystr


 3:  '     \tasdfad adfasf asdf       \t\t'


 4:  #去除空白符


 5:  >>> mystr.strip()


 6:  'asdfad adfasf asdf'


 7:  


 8:  #去除指定字符


 9:  >>> mystr.strip('\t')


10:  '     \tasdfad adfasf' asdf       '


11:  >>> 


12:  


4.7 translate


说明:translate是单字替换,可以同时替换多个字符

例子:

1:  >>> table = str.maketrans('cs', 'kz')


2:  >>> table


3:  {115: 122, 99: 107}


4:  >>> "Please don't knock at my door!".translate(table)


5:  "Pleaze don't knokk at my door!"


6:  


--结束END--

本文标题: python字符串String模块

本文链接: https://www.lsjlt.com/news/186069.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python字符串String模块
    python的string模块1.字符串属性方法操作:1.>字符串格式输出对齐>>> str = "Python stRING"  >>> print str.center(20)         ...
    99+
    2023-01-31
    字符串 模块 python
  • Python的string模块中的Template类字符串模板用法
    string.Template() string.Template()内添加替换的字符, 使用"$"符号, 或 在字符串内, 使用"${}"; 调用时使用string.substitute(dict)函数....
    99+
    2022-06-04
    字符串 模块 模板
  • Python基础:字符串(string)
    字符串的常用操作   字符串与数组一样,支持索引操作、切片与遍历   索引、切片操作: name = 'jason' name[0] 'j' name[1:3] 'as'   遍历: for char in name: pr...
    99+
    2023-01-31
    字符串 基础 Python
  • Python二分查找+字符串模板+textwrap模块,
    目录二分查找字符串模板textwrap 模块按照空格统计词组个数用 “0” 填充字符串前言: 这个系列的专栏是为了保持 Python 手感而创建的,也可以用来...
    99+
    2022-11-11
  • python中根据字符串导入模块modu
    需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' datetime_module = importlib.import_module(...
    99+
    2023-01-30
    字符串 模块 python
  • python判断字符串(string)是
    (转载)http://outofmemory.cn/code-snippet/14513/python-decide-charaeter--string-if-contain-contains--charaeter-method 方法...
    99+
    2023-01-31
    字符串 python string
  • Python的文本常量与字符串模板之string库
    一、前言 在程序中,有很多高效率的字符串处理方式,如果开发者能够完全掌握这些高效的字符串处理,往往在开发者也能事半功倍。比如针对于字符串的处理,也是自然语言处理的基础知识。 而python3中,处理字符串的库为:st...
    99+
    2022-06-02
    Python string库 python模块
  • Python中字符串String的基本内
    首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符串常用的几种字符串内置函数(本文中牵扯到了模块与一些之前章节没讲过的相关知识,坑我之后会填...
    99+
    2023-01-31
    字符串 Python String
  • Python二分查找+字符串模板+textwrap模块实例分析
    今天小编给大家分享一下Python二分查找+字符串模板+textwrap模块实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一...
    99+
    2023-06-30
  • Python格式化字符串f-string简介
    目录简介用法简单使用表达式求值与函数调用引号、大括号与反斜杠多行f-string综合示例lambda表达式简介 f-string,亦称为格式化字符串常量(formatted stri...
    99+
    2022-12-19
    Python格式化字符串f-string Python格式化字符串 Python字符串f-string
  • Python标准数据类型-String(字符串)
    ✅作者简介:CSDN内容合伙人、阿里云专家博主、51CTO专家博主、新星计划第三季python赛道Top1 📃个人主页:hacker707的csdn博客 🔥系列专栏...
    99+
    2023-09-03
    python 字符串 原力计划
  • python3 re模块正则匹配字符串中
    匹配时间: # -*- coding:utf-8 -*- import re def parseDate(l): patternForTime = r'(\d{4}[\D]\d{1,2}[\D]\d{1,2}[\D])' ...
    99+
    2023-01-31
    正则 字符串 模块
  • Python中有哪些文本常量与字符串模板string库
    本篇文章给大家分享的是有关Python中有哪些文本常量与字符串模板string库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、前言在程序中,有很多高效率的字符串处理方式,如...
    99+
    2023-06-15
  • Python中bytes字节串和string字符串之间如何转换
    这篇文章主要介绍了Python中bytes字节串和string字符串之间如何转换,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。背景在工作中经常会碰到字节串(bytes)与字符...
    99+
    2023-06-28
  • python怎么用f-string来连接字符串
    这篇文章主要为大家展示了“python怎么用f-string来连接字符串”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“python怎么用f-string来连接字...
    99+
    2022-10-19
  • Python f-string字符串格式化的方式
    本篇内容主要讲解“Python f-string字符串格式化的方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python f-string字符串格式化的方式”吧!1、python支持字符串格...
    99+
    2023-06-20
  • Java的String(字符串详解)
    字符串 1.字符串的常见构造方法 主要有三种,一种是直接使用常量去构造,要么使用new String来构造,或者还可以使用字符数组的形式。 public static void main(String...
    99+
    2023-10-19
    java String 字符串 详解
  • C# String字符串的用法
    本篇内容主要讲解“C# String字符串的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C# String字符串的用法”吧!string是一种很特殊的数据类型,它既是基元类型又是引用类型,...
    99+
    2023-06-20
  • string字符串如何赋值
    字符串可以通过以下几种方式进行赋值:1. 直接赋值:可以使用双引号或单引号将字符串括起来,赋值给变量。```pythons = "H...
    99+
    2023-08-18
    string
  • python字符串
    字符串的操作方法很多,这里只选几种常用的(1)    字符串大小写转换1.    S.lower()    字母大写转换...
    99+
    2023-01-30
    字符串 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作