极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

了解Python中的字符串是什么吗?

极客小将2021-03-16-
简介摘要:本文将告诉您Python中的字符串是什么,并向您简要介绍有关该概念的所有知识。本文将介绍以下内容:如何创建一个字符串?如何从字符串访问字符?格式化字符串因此,让我们开始吧。什么是Python中的字符串?我们许多熟悉C,C++等编程语言的人都会得到诸如“字符串是字符的集合或字符数组”的答案。在P
aM6少儿编程网-https://www.pxcodes.com

摘要:本文将告诉您python中的字符串是什么,并向您简要介绍有关该概念的所有知识。

本文将介绍以下内容:aM6少儿编程网-https://www.pxcodes.com

如何创建一个字符串?如何从字符串访问字符?格式化字符串

因此,让我们开始吧。aM6少儿编程网-https://www.pxcodes.com

什么是Python中的字符串?aM6少儿编程网-https://www.pxcodes.com

我们许多熟悉C,C ++等编程语言的人都会得到诸如“字符串是字符的集合或字符数组”的答案。aM6少儿编程网-https://www.pxcodes.com

在Python中也是如此,我们说的是String数据类型的相同定义。字符串是序列字符的数组,并写在单引号,双引号或三引号内。另外,Python没有字符数据类型,因此当我们编写“ a”时,它将被视为长度为1的字符串。aM6少儿编程网-https://www.pxcodes.com

继续本文,了解什么是Python中的String?aM6少儿编程网-https://www.pxcodes.com

如何创建一个字符串?aM6少儿编程网-https://www.pxcodes.com

s = 'Hello' print(s) s1 = "Hello" print(s1) s2 = ''' Hello How is the whether today? ''' print(s2)

输出:aM6少儿编程网-https://www.pxcodes.com

你好
你好
你好
今天如何?aM6少儿编程网-https://www.pxcodes.com

当我们在字符串中同时使用单引号和双引号以及要编写多行句子时,通常使用三引号。aM6少儿编程网-https://www.pxcodes.com

笔记aM6少儿编程网-https://www.pxcodes.com

我们需要注意的是,在使用单引号时,字符串中不应包含单引号,因为如果发生这种情况,Python将假定该行在第二个引号本身出现的情况下结束,并且不会获取所需的输出。相同的符号后应加上双引号和三引号。aM6少儿编程网-https://www.pxcodes.com

继续本文,了解什么是Python中的String?aM6少儿编程网-https://www.pxcodes.com

如何从字符串访问字符?aM6少儿编程网-https://www.pxcodes.com

假设我们要访问字符串中的一个字符,比方说**后一个字符,我们需要知道它在字符串中的位置。aM6少儿编程网-https://www.pxcodes.com

aM6少儿编程网-https://www.pxcodes.com

这是一个字符串以及分配的位置。因此,如果要从字符串访问'n',则必须转到第5位。aM6少儿编程网-https://www.pxcodes.com

编号或索引从0到小于字符串长度的1开始。aM6少儿编程网-https://www.pxcodes.com

这是一个python程序,可以使我们更加清楚。aM6少儿编程网-https://www.pxcodes.com

str = 'Antarctica is really cold.' print('str = ', str) #first character print('str[0] = ', str[0]) #last character print('str[-1] = ', str[-1]) #slicing 2nd to 5th character print('str[1:5] = ', str[1:5]) #slicing 6th to 2nd last character print('str[5:-2] = ', str[5:-2])

输出:aM6少儿编程网-https://www.pxcodes.com

str =南极洲真的很冷。
str [0] = A
str [-1] =。
str [1:5] = ntar
str [5:-2] = ctica确实是colaM6少儿编程网-https://www.pxcodes.com

现在,如果在索引中从左到右遵循递增顺序模式,则从右到左遵循递减顺序模式,即从-1,-2,-3等。因此,如果要访问**后一个字符,可以通过两种方式进行。aM6少儿编程网-https://www.pxcodes.com

str = 'Antarctica is really cold.' a = len(str) print('length of str ', a) #last character with the help of length of the string print('str[a] ', str[a-1]) #last character with the help of indexing print('str[-1] ',str[-1])

输出:aM6少儿编程网-https://www.pxcodes.com

str 26
str [a]的长度。
str [-1]。aM6少儿编程网-https://www.pxcodes.com

字符串本质上是不可变的,这意味着一旦声明了字符串,就不能更改其中的任何字符。aM6少儿编程网-https://www.pxcodes.com

s = "Hello Batman" print(s) s[2] = 'P' print(s)

输出:aM6少儿编程网-https://www.pxcodes.com

你好蝙蝠侠
回溯(**近一次通话**近):
文件“ C:/Users/prac.py”,第3行,位于
s [2] =' P'TypeError
:'str'对象不支持项目分配aM6少儿编程网-https://www.pxcodes.com

流程以退出代码1完成aM6少儿编程网-https://www.pxcodes.com

但是,您可以使用del运算符删除整个字符串。aM6少儿编程网-https://www.pxcodes.com

s = "Hello Batman" print(s) del s print(s)

输出:aM6少儿编程网-https://www.pxcodes.com

您好蝙蝠侠
回溯(**近一次通话**近):
文件“ C:/Users/prac.py”,
打印中的第4行
NameError:未定义名称“ s”aM6少儿编程网-https://www.pxcodes.com

流程以退出代码1完成aM6少儿编程网-https://www.pxcodes.com

如果您不希望s是“ Hello Batman”,而希望它是其他字符串,则可以将字符串整体进行更新。aM6少儿编程网-https://www.pxcodes.com

s = "Hello Batman" print(s) s = "Hello Spiderman" print(s)

输出:aM6少儿编程网-https://www.pxcodes.com

你好蝙蝠侠
你好蜘蛛侠aM6少儿编程网-https://www.pxcodes.com

继续本文,了解什么是Python中的String?aM6少儿编程网-https://www.pxcodes.com

格式化字符串:aM6少儿编程网-https://www.pxcodes.com

格式化字符串意味着可以在任意位置动态分配字符串。aM6少儿编程网-https://www.pxcodes.com

可以使用 format()方法来格式化Python中的字符串,该方法是用于格式化字符串的功能非常强大的工具。String中的Format方法包含大括号{}作为占位符,可以根据位置或关键字保存参数以指定顺序。aM6少儿编程网-https://www.pxcodes.com

String1 = "{} {} {}".format('Hello', 'to', 'Batman') print("Default order: ") print(String1) # Positional Formatting String1 = "{1} {0} {2}".format('Hello', 'to', 'Batman') print("nPositional order: ") print(String1) # Keyword Formatting String1 = "{c} {b} {a}".format(a='Hello', b='to', c='Spiderman') print("nString in order of Keywords: ") print(String1) # Formatting of Integers String1 = "{0:b}".format(20) print("binary representation of 20 is ") print(String1) # Formatting of Floats String1 = "{0:e}".format(188.996) print("nExponent representation of 188.996 is ") print(String1) # Rounding off Integers String1 = "{0:.2f}".format(1 / 6) print("none-sixth is : ") print(String1) # String alignment String1 = "|{:<10}|{:^10}|{:>10}|".format('Hello', 'to', 'Tyra') print("nLeft, centre and right alignment with Formatting: ") print(String1)

输出:aM6少儿编程网-https://www.pxcodes.com

默认顺序:
蝙蝠侠向您问好aM6少儿编程网-https://www.pxcodes.com

位置顺序:
致Hello BatmanaM6少儿编程网-https://www.pxcodes.com

字符串按关键字顺序排列:
蜘蛛侠到HelloaM6少儿编程网-https://www.pxcodes.com

20的二进制表示形式是10100aM6少儿编程网-https://www.pxcodes.com

188.996的指数表示为
1.889960e + 02aM6少儿编程网-https://www.pxcodes.com

六分之一是:
0.17aM6少儿编程网-https://www.pxcodes.com

左对齐,居中对齐和右对齐,格式为:
|您好| 到| 泰拉|aM6少儿编程网-https://www.pxcodes.com

可以使用格式方法将字符串左对齐(<),右对齐(>)或居中(^)。aM6少儿编程网-https://www.pxcodes.com

{:<10} .format(“ Hello”)表示Python将为该字符串保留10个空间,并且该字符串将从左侧开始。右对齐和居中对齐也是如此。aM6少儿编程网-https://www.pxcodes.com

我希望您能很好地学习这些概念,并尝试使其更加准确。aM6少儿编程网-https://www.pxcodes.com

相关免费学习推荐:python视频教程aM6少儿编程网-https://www.pxcodes.com

以上就是了解Python中的字符串是什么吗?的详细内容,更多请关注少儿编程网其它相关文章!aM6少儿编程网-https://www.pxcodes.com

网友点评

共有5条评论来说两句吧...

在线客服