菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python3 strip()方法

Python3 字符串 Python3 字符串


描述

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

语法

strip()方法语法:

str.strip([chars]);

参数

  • chars -- 移除字符串头尾指定的字符序列。

返回值

返回移除字符串头尾指定的字符序列生成的新字符串。

实例

以下实例展示了 strip() 函数的使用方法:

实例(Python 3.0+)

#!/usr/bin/python3str = "*****this is **string** example....wow!!!*****"print(str.strip('*'))# 指定字符串 *

以上实例输出结果如下:

this is **string** example....wow!!!

从结果上看,可以注意到中间部分的字符并未删除。

以上下例演示了只要头尾包含有指定字符序列中的字符就删除:

实例(Python 3.0+)

#!/usr/bin/python3str = "123abcrunoob321"print(str.strip('12'))# 字符序列为 12

以上实例输出结果如下:

3abcrunoob3

Python3 字符串 Python3 字符串

AI 思考中...

3 篇笔记 写笔记

  1. #0

    uswood

    usw***@163.com

    95

    1、strip() 处理的时候,如果不带参数,默认是清除两边的空白符,例如:/n, /r, /t, ' ')。

    2、strip() 带有参数的时候,这个参数可以理解一个要删除的字符的列表,是否会删除的前提是从字符串最开头和最结尾是不是包含要删除的字符,如果有就会继续处理,没有的话是不会删除中间的字符的。

    addr = '[email protected]'
    addr1 = addr.strip('12')
    以上例子因为 1[email protected] 的左边第一个,所以删除了继续判断,2 也存在,所以也删除。结果为:
    [email protected] 

    如果要删除的字符列表不包含第一个字符呢?

    addr = '[email protected]'addr1 = addr.strip('23')

    此时 2 不是第一个字符,所以无法继续,结果为:

    [email protected] 

    uswood

    usw***@163.com

    8年前 (2018年07月08日)
  2. #0

    礼赞

    103***[email protected]

    103

    注意删除多个字符时:只要头尾有对应其中的某个字符即删除,不考虑顺序,直到遇到第一个不包含在其中的字符为止。

    示例如下:

    str = '123132231213321312==321312213231123132'
    print(str.strip('123'))

    以上代码输出结果为:

    ==

    礼赞

    103***[email protected]

    8年前 (2018年11月29日)
  3. #0

    古怪的乐天派

    140***[email protected]

    37

    接上面两位的补充

    充分理解这句:删除多个字符时只要头尾有对应其中的某个字符即删除,不考虑顺序,直到遇到第一个不包含在其中的字符为止。

    1.只有其中一个数字且在开头。

    addr = '[email protected]'
    addr1 = addr.strip('12')

    结果:

    addr1='[email protected]'

    2.

    addr = '1111111111113'
    addr1 = addr.strip('12')
    addr1='3'

    3.

    addr = '22222211113'
    addr1 = addr.strip('12')

    结果:

    addr1 = '3'

    古怪的乐天派

    140***[email protected]

    6年前 (2020年11月08日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /