侧边栏壁纸
  • 累计撰写 119 篇文章
  • 累计创建 25 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

字符串操作

梁来福
2022-02-02 / 0 评论 / 0 点赞 / 0 阅读 / 1591 字
温馨提示:
本文最后更新于 2024-05-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

str = 'hello world and hello python and hello java'

1、capitalize() ---字符串字母大写

new_str = str.capitalize()
print(new_str)

执行结果:Hello world and hello python and hello java

2、title() ---字符串中每个单词首字母大写

new_str = str.title()
print(new_str)

执行结果:Hello World And Hello Python And Hello Java

3、upper() ---小写转大写

new_str = str.upper()
print(new_str)

执行结果:HELLO WORLD AND HELLO PYTHON AND HELLO JAVA

4、lower() ---大写转小写

new_str = str.lower()
print(new_str)

执行结果:hello world and hello python and hello java


删除字符串空白字符

1、lstrip() ---删除字符串左侧的空白字符

2、rstrip() ---删除字符串右侧的空白字符

3、strip() ---删除字符串两侧的空白字符


字符串对齐

语法:字符串序列.函数(长度,填充字符)

1、ljust() ---左对齐

2、rjust() ---右对齐

3、center() ---居中对齐

image.png


判断字符串开头结尾

语法:字符串序列.函数(子串,开始位置下标,结束位置下标)

1、startswith() ---判断字符串是否以某个字段开头

2、endswith() ---判断字符串是否以某个字段结尾


判断字符串

1、isalpha() ---判断字符串中都是字母

2、isdigit() ---判断字符串中都是数字

3、isalnum() ---判断字符串中都是数字或者字母

4、isspace() ---判断字符串中都是空白

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
博主关闭了所有页面的评论