Python 语言中的String

在Python中,String代指以下特点:

代表Unicode字符的字节数组

用单引号或双引号引起来

无限长度

Python 中 String 字符串定义方式

$title(单行文字的定义方法)

str = 'hello world'

str = "hello world"

一个多行字符串使用三个单引号或三个双引号创建:

$title(多行文字的定义方法)

str = '''Say hello

to python

programming'''

str = """Say hello

to python

programming"""

Python没有字符数据类型,单个字符就是长度为1的字符串。

python String 字符串截取 /分割

通过使用slice语法,我们可以获得一组字符。索引从零开始。

str[m:n] 从位置2(包括)到5(不包括)返回字符串。

$title(截取字符串索引2到5部分)

str = 'hello world'

print(str[2:5]) # llo

负切片将从末尾返回子字符串:

$title(截取字符串从索引-5到-2部分)

str = 'hello world'

print(str[-5:-2]) # wor

str[-m:-n] 将字符串从位置-5(不包括)返回到-2(包括)。

python中的字符串数组

在python中,字符串表现为数组。方括号可用于访问字符串的元素

tr = 'hello world'

print(str[0]) # h

print(str[1]) # e

print(str[2]) # l

print(str[20]) # 错误的索引: string index out of range

python字符串长度计算

len()函数返回字符串的长度:

str = 'hello world'

print(len(str)) # 11

python字符串格式化

要在python中格式化s字符串,请{ }在所需位置在字符串中使用占位符。将参数传递给format()函数以使用值格式化字符串。

我们可以在占位符中传递参数位置(从零开始)。

age = 36

name = 'Lokesh'

txt = "My name is {} and my age is {}"

print(txt.format(name, age)) # My name is Lokesh and my age is 36

txt = "My age is {1} and the name is {0}"

print(txt.format(name, age)) # My age is 36 and the name is Lokesh

python字符串其他内置函数方法

1. 1.capitalize() 大写转换

它返回一个字符串,其中给定字符串的第一个字符被转换为大写。当第一个字符为非字母时,它将返回相同的字符串。

name = 'lokesh gupta'

print( name.capitalize() ) # Lokesh gupta

txt = '38 yrs old lokesh gupta'

print( txt.capitalize() ) # 38 yrs old lokesh gupta

1.2. casefold()小写转换

它返回一个字符串,其中所有字符均为给定字符串的小写字母。

txt = 'My Name is Lokesh Gupta'

print( txt.casefold() ) # my name is lokesh gupta

1.3. center()中间对其方法

使用指定的字符(默认为空格)作为填充字符,使字符串居中对齐。

在给定的示例中,输出总共需要20个字符,而“ hello world”位于其中。

txt = "hello world"

x = txt.center(20)

print(x) # ' hello world '

1.4. count() 计算

它返回指定值出现在字符串中的次数。它有两种形式:

count(value) - value to search for in the string.

count(value, start, end) - value to search for in the string, where search starts fromstart position till end position.

txt = "hello world"

print( txt.count("o") ) # 2

print( txt.count("o", 4, 7) ) # 1

1.5. encode() 编码

它使用指定的编码对字符串进行编码。如果未指定编码,UTF-8将使用。

txt = "My name is åmber"

x = txt.encode()

print(x) # b'My name is \xc3\xa5mber'

1.6. endswith() 判断字符串结尾

rue如果字符串以指定值结尾,则返回,否则返回False。

txt = "hello world"

print( txt.endswith("world") ) # True

print( txt.endswith("planet") ) # False

1.7. expandtabs()

它将制表符大小设置为指定的空格数。

txt = "hello\tworld"

print( txt.expandtabs(2) ) # 'hello world'

print( txt.expandtabs(4) ) # 'hello world'

print( txt.expandtabs(16) ) # 'hello world'

1.8. find() 字符串查找

它查找指定值的第一次出现。-1如果字符串中没有指定的值,它将返回。

find()与index()方法相同,唯一的区别是,index()如果找不到该值,该方法将引发异常。

txt = "My name is Lokesh Gupta"

x = txt.find("e")

print(x) # 6

1.9. format()格式化

它格式化指定的字符串,并在字符串的占位符内插入参数值。

age = 36

name = 'Lokesh'

txt = "My name is {} and my age is {}"

print( txt.format(name, age) ) # My name is Lokesh and my age is 36

1.10. format_map()

它用于返回字典键的值,以格式化带有命名占位符的字符串。

params = {'name':'Lokesh Gupta', 'age':'38'}

txt = "My name is {name} and age is {age}"

x = txt.format_map(params)

print(x) # My name is Lokesh Gupta and age is 38

1.11. index() 索引

它在给定的字符串中查找指定值的第一次出现。

如果找不到要搜索的值,则会引发异常。

txt = "My name is Lokesh Gupta"

x = txt.index("e")

print(x) # 6

x = txt.index("z") # ValueError: substring not found

1.12. isalnum()

它检查字母数字字符串。True如果所有字符都是字母数字,即字母(a-zA-Z)和数字,它将返回(0-9)

print("LokeshGupta".isalnum()) # True

print("Lokesh Gupta".isalnum()) # False - Contains space

1.13. isalpha()

True如果所有字符都是字母,则返回它,即字母(a-zA-Z)。

print("LokeshGupta".isalpha()) # True

print("Lokesh Gupta".isalpha()) # False - Contains space

print("LokeshGupta38".isalpha()) # False - Contains number

1.14. isdecimal()

如果所有字符均为小数(0-9),则返回代码。否则返回False。

print("LokeshGupta".isdecimal()) # False

print("12345".isdecimal()) # True

print("123.45".isdecimal()) # False - Contains 'point'

print("1234 5678".isdecimal()) # False - Contains space

python中find函数的使用方法_Python教程-String 字符串使用教程相关推荐

  1. python中find函数的使用方法_Python学习日记5|BeautifulSoup中find和find_all的用法

    今天是4.20号. 前天晚上看到蒋方舟的一句话: 不要左顾右盼.慢慢积累,慢慢写吧.毕竟除了这样单调的努力,我什么也做不了. 而现在的自己就是个十足的壁花少年. 在进入正题前先说一下每次完成代码后,可 ...

  2. python中find函数的使用方法_python find()用法

    案例: ### 1 ### str = "01213456" if str.find("23"):print "YES!" else:pri ...

  3. python中decode函数在哪个库_python中decode函数的使用方法

    python中decode函数的使用方法 发布时间:2020-12-15 09:22:45 来源:亿速云 阅读:75 作者:小新 这篇文章主要介绍python中decode函数的使用方法,文中介绍的非 ...

  4. Python中range函数的使用方法

    Python中range函数的使用方法 更新时间:2022年05月30日 11:38:15   作者:Python编程学习圈 这篇文章主要介绍了Python中range函数的使用方法,文章基于Pyth ...

  5. 技术图文:举例详解Python中 split() 函数的使用方法

    背景 这篇文章主要介绍Python中的split()函数的使用方法,split()函数通常用于将字符串切片并转换为列表,需要的朋友可以参考一下. 技术分析 Python中有split()和os.pat ...

  6. python中sum函数的使用方法及实例_sum函数的使用方法及实例

    在我很重要的人.QQ相册上看到的英文. BRO有以下两种意思: 1.BRO是brother的简称,意思是兄弟,哥们.是口语中的简称.类似的还有"sister"姐妹的简称为" ...

  7. Python中range()函数的使用方法

    range()函数可以产生一系列的数字.当需要叠加一些数字时,可以用到range()函数. 1 基本语法 range()函数的基本语法如下所示. range(start, stop) 其中,start ...

  8. python中int函数的用法浅析_Python中int()函数的用法浅析

    int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...

  9. python中int函数是什么作用_python中int函数怎么用

    python中int函数怎么用,字符串,函数,数字,出现在,赋值 python中int函数怎么用 易采站长站,站长之家为您整理了python中int函数怎么用的相关内容. int() 函数用于将一个字 ...

最新文章

  1. SQL中LIKE的妙用
  2. Python pip切换为国内镜像源(亲测可用)
  3. OpenGL rimlight边缘照明的实例
  4. linux 查看vnc端口_VNC常用操作及常见问题解决办法汇总
  5. 5G标准化加速 需应对测试场景复杂性
  6. HTML中的行内元素和框元素详解
  7. mysql安装 linux rpm_linux MySQL5.7 rpm安装
  8. YUI3 Overlay的使用
  9. 【已解决】解锁小米6的时候,卡在当前未连接手机怎么办?
  10. 索尼电视未能找到服务器是怎么回事,索尼电视进行网络设置或尝试访问互联网内容时出现错误...
  11. APP支付宝登录第三方授权如何签约入口在哪里
  12. Python中index函数用法总结
  13. linux下USB设备编号固定
  14. 《寒江独钓》内核学习笔记(1)-- IRP - .Little Hann
  15. bzoj 3728 zarovik 买灯泡
  16. 618投影仪怎么选?看看极米NEW Z6X、极米Z6X Pro与极米H3S
  17. Windows系统合并磁盘分区
  18. 【观察】加速IPFS基础设施落地,西部数据的三重独特优势
  19. 什么是Email Bounced(被退回)
  20. 加密网站地址——从根本解决盗连

热门文章

  1. mysql cst_JDBC与mysql同为CST时区导致数据库时间和客户端时间差13或者14小时
  2. python socket 域名_python用socket发送http请求
  3. 百度之星2018资格赛t6三原色图(MST minimum spanning tree)
  4. 解决QT编程出现 C2001错误
  5. 关于crossvalind函数(转)
  6. 自定义配置节 Section
  7. 队列,链队列,链式存储的队列
  8. uc浏览器邀请码_UC密保手机不能用?冬树教你如何一招申诉成功!
  9. Ubuntu18.04 安装wine
  10. Android与Chromium源码搜索工具