Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Python Day 字符串函数_创想鸟

Python Day 字符串函数

python day 字符串函数

python 是动态类型编程语言,这意味着不需要输入数据类型。相反,python 将默认采用数据类型。
它也被称为鸭子类型编程语言。

供参考:https://docs.python.org/3/library/string.html
https://peps.python.org/pep-0020/

字符串:

string(str) 指的是应该用 ‘ ‘ 单引号或 ” ” 双引号表示的单词。

例如:

city = 'madurai's jigarthanda is very famous'print(city)

对于上述输入,输出将为

syntaxerror: unterminated string literal

因此,即使字符串是在 ‘ ‘ 单引号中定义的,也会显示语法错误。原因是字符串不完整 madurai’s 在这个单词中我们使用 ‘ 撇号,但 python 认为这是一个字符串并且它是不完整的.

可以通过使用三个连续的 ”’ ”’ 或 “”” “”” 来纠正此错误。

立即学习“Python免费学习笔记(深入)”;

例如:

city = '''madurai's jigarthanda is very famous'''print(city)city2= """madurai's jigarthanda is very famous"""print(city2)address = """no. 7, east street,             mela masi veedhi,            madurai 625002"""print(address)

对于上述输入,输出将为

madurai's jigarthanda is very famousmadurai's jigarthanda is very famousno. 7, east street,             mela masi veedhi,            madurai 625002

即使对于多行字符串,您也应该使用 “”” “”” 双引号。

1.在python中一切都是对象。
2.每个对象都有自己的内存空间。
3.字符串是不可变的。
例如:

name = 'guru'degree = 'b.com'height = 170sunday = falseprint(id(name))print(id(degree))print(id(height))print(id(sunday))

输出将为

1290093335952481290093356599681175930410654592

因此 print(id(#对象名称)) 将用于查找对象的内存。

索引/下标:(வரிசை)
例如:
大师
0123

因此索引用于访问单个字符。在上面的示例中为 g-0,u-1,r-2,u-3。
索引总是从零开始。

例如:1

name = 'guru'print(name[0])print(name[1])print(name[2])print(name[3])

输出:

guru

例如:2

name = 'guru'print(name[0],end=' ')print(name[1],end=' ')print(name[2],end=' ')print(name[3],end=' ')

输出:

g u r u

对于水平输出,请使用 end=’ ‘。如果未使用 end,则输出将像 ex:1 一样垂直输出。

= 用于赋值
== 用于比较。

字符串函数:
print(len(#object)) – 用于查找字符串的长度。

ame = 'guru'# first letterprint(name[0])#last letterprint(name[3])#first letter 'g'if name[0] == 'g':    print("yes starts with g")#last letter 'u'if name[3] == 'u':    print("yes ends with u")#all letters with single space in same lineprint(name[0],end=' ')print(name[1],end=' ')print(name[2],end=' ')print(name[3],end='n')#middle letterlength=len(name)#4print(name[length//2])

上面的例子中 // 用于除法,称为向下除法。它不会取小数点值。
atlast n 用于换行。
round()-用于四舍五入小数点。

输出:

guyes starts with gyes ends with ug u r ur

几个字符串函数:

capitalize() – 将第一个字符转换为大写
casefold() – 将字符串转换为小写
endswith() – 如果字符串以指定值结尾则返回 true

例如:

print(name.capitalize())name = 'guruprasanna'print(name.casefold())print(name.endswith('prasanna'))

输出:

Guru came to class todayguruprasannaTrue

以上就是Python Day 字符串函数的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354102.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Pygame子弹长度不变?可能是参数设置错误
上一篇 2025年12月13日 18:16:02
Python正则表达式如何准确统计Go语言文件中的类、属性和方法数量?
下一篇 2025年12月13日 18:16:18

相关推荐

发表回复

登录后才能评论
关注微信