Python - 去除字符串首尾填充
16lz
2021-01-22
去除字符串首尾填充
本文地址:http://blog.csdn.net/caroline_wendy/article/details/20464251
Python中, 去除字符串首尾填充, 使用lstrip(), 去除左填充;rstrip(), 去除右端填充;strip(), 去除两端填充;
也可以指定填充去除, 注意只会去除首尾两端的填充, 以空格为标记;
代码如下:
# -*- coding: utf-8 -*-
#====================
#File: TextExercise.py
#Author: Wendy
#Date: 2014-03-04
#====================
#eclipse pydev, python2.7
x = ' hej '
print('|' + x.lstrip() + '|' + x.rstrip() + '|' + x.strip() + '|')
y = 'xyxyyy hejxy yyx'
print('|' + y.strip('xy') + '|') #只移除开头和结尾的xy
输出:
|hej | hej|hej|| hejxy |
更多相关文章
- Python处理字符串
- python list range 字符串的截取 如 text[1:5]
- 在Python中使用正则表达式匹配的字符串周围添加括号
- 用python将二进制整数或字符串写入文件
- python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能
- Python笔记(九):字符串操作
- python 产生随机数,随机字符串
- linux shell脚本编程笔记(四): 获取字符串长度的七种方法
- Linux Shell编程(15)——操作字符串