心蓝的博客 心蓝的博客
首页
  • 零基础

    • python零基础入门
  • 专项

    • 正则表达式
  • web框架

    • django框架
    • drf
技术
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档

心蓝

人生苦短,我用python
首页
  • 零基础

    • python零基础入门
  • 专项

    • 正则表达式
  • web框架

    • django框架
    • drf
技术
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • 编程基础

  • python开发环境搭建

  • 基本数据类型

    • 基本数据类型概述
    • 变量的定义和使用
    • 整数类型
    • 浮点数类型
    • 算术运算符
    • 赋值运算符
    • 字符串类型
    • 字符串常用方法
    • 字符串格式化
    • 列表类型
    • 元组类型
    • 12 可变与不可变对象
    • 13 深浅拷贝
    • 14 集合类型
    • 字典类型
    • 布尔型
    • None
  • 程序流程控制

  • 函数与代码复用

  • 面向对象

  • 模块和包

  • 文件IO操作

  • python零基础入门
  • 基本数据类型
心蓝
2022-12-14

字符串常用方法

# 字符串常用方法

通过内建函数dir可以返回传入其中对象的所有方法属性名列表。

>>> print(dir(str))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
1
2

所有不以__开头的名字就是字符串可以使用的方法。关于什么是方法这概念,我会在后面面向对象里给大家介绍。

这里给大家介绍一些常用的字符串方法。

  • .capitalize()

生成字符串标题化后的副本。第一个字母大写,其他字母小写。

>>> 'hello'.capitalize()
'Hello'
>>> 'HELLO'.capitalize()
'Hello'
1
2
3
4
  • .islower()

判断字符串是否是全小写形式。

>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
1
2
3
4
  • .isupper()

判断字符串是否是全大写形式。

>>> 'Hello'.isupper()
False
>>> 'HELLO'.isupper()
True
1
2
3
4
  • .lower()

返回字符串全小写的副本。

>>> 'HellO'.lower()
'hello'
1
2
  • .upper()

返回字符串全大写的副本。

>>> 'hello'.upper()
'HELLO'
1
2
  • .replace(old,new,count=-1)

返回字符串替换后的副本。接收三个参数。

old需要被替换部分的字符串

new替换old的字符串

count替换次数,默认为-1表示替换所有。

>>> phone = '027-8765-5671'		# 将`-`替换掉
>>> phone = phone.replace('-', '', 1) # 只替换一次
>>> print(phone)
0278765-5671
>>> phone = '027-8765-5671'		# 将`-`替换掉
>>> phone = phone.replace('-', '') # 替换所有
>>> print(phone)
02787655671
1
2
3
4
5
6
7
8
  • .strip()

返回字符串删除首尾不可见字符后的副本。

>>> ' hello world  '.strip()
'hello world'
1
2

总结一下,字符串方法的调用格式是:字符串.方法名(参数)。调用字符串方法不会改变原字符串,只会返回字符串的副本,或者判断结果。

>>> a = 'hello'
>>> a.isupper()   	# 返回大写副本
'HELLO'
>>> a				# 原字符串不变
'hello'
1
2
3
4
5

更多字符串方法,大家可以在学完函数和面向对象之后再去参考官方文档地址 (opens new window)。

本文完,感谢你的耐心阅读,如有需要可加我微信,备注「博客」并说明原因,我们一起进步,下次见。

#python#零基础
上次更新: 2022/12/15, 21:20:30
字符串类型
字符串格式化

← 字符串类型 字符串格式化→

最近更新
01
requests让接口测试如此简单 原创
03-31
02
最简明的python正则教程
03-30
03
pycharm激活码
12-30
更多文章>
Theme by Vdoing | Copyright © 2019-2025 心蓝
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式