码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python字符串常用方法介绍,基于python3.10


     python字符串常用方法-目录:

    1、strip()、lstrip()、rstrip()
    2、removeprefix()、removesuffix()
    3、replace()
    4、split()、rsplit()
    5、join()
    6、upper()、lower()、capitalize()
    7、islower()、isupper()、isalpha()、isnumeric()、isalnum()
    8、count()
    9、find()、rfind()
    10、startswith()、endswith()
    11、partition()
    12、center()、ljust()、rjust()
    13、字符串专用f表达式
    14、swapcase()
    15、zfill()

    - - - - - - - - - - 分割线- - - - - - - - - - 

    1、strip()、lstrip()、rstrip()

    作用:去除两边空格、左边空格、右边空格

    s = "   abcd   "
    print("|"+s.strip()+"|")
    print("|"+s.lstrip()+"|")
    print("|"+s.rstrip()+"|")

    查看运行结果:

     2、removeprefix()、removesuffix()

    作用:移除前缀、移除后缀

    s = "hello:world"
    print(s.removeprefix("hello"))
    print(s.removesuffix("world"))

    查看运行结果:

     3、replace()

    作用:替换字符串中的内容替换成指定的内容

    s = "hello:world"
    s = s.replace(":", "-")
    print(s)

    查看运行结果:

     4、split()、rsplit()

    作用:从左边起根据对用的内容分割字符串、从右边起根据对用的内容分割字符串(当指定字符串的分隔次数时存在区别)

    复制代码
    s = "hello:world:ok"
    print(s.split(":"))
    print(s.rsplit(":"))
    print(s.split(":", maxsplit=1))
    print(s.rsplit(":", maxsplit=1))
    复制代码

    查看运行结果:

     5、join()

    作用:将括号内的元素(均需要满足字符串格式)合并成一个新的字符串,已join前的字符作为分隔符

    l = ["hello", "1", "world"]
    print("".join(l))
    print("-".join(l))

    查看运行结果:

    6、upper()、lower()、capitalize()

    作用:将所有字母转为大写、将所有字母转为小写、将首字母转为大写

    s = "helloWORLD"
    print(s.upper())
    print(s.lower())
    print(s.capitalize())

    查看运行结果:

     7、islower()、isupper()、isalpha()、isnumeric()、isalnum()

    作用:检查字符串中字母是否都为小写、检查字符串中字母是否都为大写、检查字符串中字符是否都是字母、检查字符串中字符是否都是数字、检查所有的字符串是否都是数字或字母

    复制代码
    s1 = "helloworld"
    s2 = "OK"
    s3 = "hello OK"
    s4 = "567"
    s5 = "hello123"

    print(s1.islower())
    print(s2.isupper())
    print(s3.islower(), s3.isupper())
    print(s1.isalpha())
    print(s4.isnumeric())
    print(s5.isalpha(), s5.isnumeric())
    print(s5.isalnum())
    print(s3.isalnum())
    复制代码

    查看运行结果:

    8、count()

    作用:返回指定内容在字符串中出现的次数

    s = "hello world"
    print(s.count("o"))

    查看运行结果:

    9、find()、rfind()

    作用:返回字符串中是否包含指定内容的索引信息(从左边开始第一个出现的),不包含时返回-1、返回字符串中是否包含指定内容的索引信息(从右边开始第一个出现的),不包含时返回-1

    s = "hello world"
    print(s.find("x"))
    print(s.find("o"))
    print(s.rfind("o"))

    查看运行结果:

     10、startswith()、endswith()

    作用:检查字符串是否是以指定内容开头、检查字符串是否是以指定内容结束

    s = "hello world"
    print(s.startswith("h"), s.endswith("h"))
    print(s.startswith("d"), s.endswith("d"))

    查看运行结果:

     11、partition()

    作用:有点像find()和split()的结合体。将字符串根据指定的内容拆分为三个元素的元祖,其中第二个元素为指定的内容,如果不包含指定的内容的话,返回的第一个元素为原字符串

    s = "hello world"
    print(s.partition(" "))
    print(s.partition("hello"))
    print(s.partition("123"))

    查看运行

     12、center()、ljust()、rjust()

    作用:

    返回一个原字符串居中,并使用指定内容(默认为空格)填充至长度width的新字符串

    返回一个原字符串左对齐,并使用指定内容(默认为空格)填充至长度width的新字符串

    返回一个原字符串右对齐,并使用指定内容(默认为空格)填充至长度width的新字符串。

    复制代码
    s = "python"
    print(s.center(30))
    print(s.center(30, "-"))
    print(s.ljust(30, "-"))
    print(s.rjust(30, "-"))
    复制代码

    查看运行结果:

    13、字符串专用f表达式

    作用:是格式化字符串的新语法,更易读,更简洁,不易出错,而且速度更快!需要python3.6+的版本支持

    name = "bk"
    age = 15
    print(f"my name is {name},and i am {age} years old!")

    查看运行结果:

    14、swapcase()

    作用:翻转字符串中的字母大小写

    name = "My Name is Mr.white"
    print(name.swapcase())

    查看运行结果:

    15、zfill()

    作用:返回长度为width的字符串,原字符串string右对齐,前面填充0

    print("100".zfill(5))
    print("+100".zfill(5))
    print("-100".zfill(5))
    print("+0010".zfill(5))

    查看运行结果:

     

     

     

  • 相关阅读:
    承前启后,Java对象内存布局和对象头
    详解【计算机类&面试真题】军队文职考试——第8期:OSI的七层模型 | 数据通信的三种方式 | 通信网络的检查方法,附Python进行网络连通性检查、带宽测试、端口扫描、链路质量测试、安全性扫描
    微信小程序全局水印组件
    如何设计日志内容
    测试用例千万不能随便,记录由一个测试用例异常引起的思考
    uniapp微信小程序自定义封装分段器。
    VoxEdit 主题创作大赛:将 90 年代的复古元素带入 Web3
    Win10 OpenCV编译安装CUDA版本
    Django ORM深度游:探索多对一、一对一与多对多数据关系的奥秘与实践
    Java——ArrayList类的常用方法
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/16949270.html
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号