【 Python进阶】python strip() split()函数实战

本文详细介绍了Python中strip()和split()函数的使用方法,包括去除字符串两端的指定字符及按指定字符分割字符串等常见操作,并通过实例展示了如何利用这两个函数进行IP地址的有效性检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转载自:Python进阶---python strip() split()函数实战

先看一个例子:

>>> ipaddr = 10.122.19.10
  File "", line 1
    ipaddr = 10.122.19.10
                     ^
SyntaxError: invalid syntax
>>> ipaddr = "10.122.19.10"
>>> ipaddr.strip()
'10.122.19.10'
>>> ipaddr = '10.122.19.10'
>>> ipaddr.strip()
'10.122.19.10'
>>> ipaddr.split('.')
['10', '122', '19', '10']
>>> ipaddr.strip().split('.')
['10', '122', '19', '10']
>>> 

python strip()函数 介绍

函数原型

声明:s为字符串,rm为要删除的字符序列

s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符

s.lstrip(rm)       删除s字符串中开头处,位于 rm删除序列的字符

s.rstrip(rm)      删除s字符串中结尾处,位于 rm删除序列的字符

注意:

1. 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')

例如:

 

复制代码 代码如下:

>>> a = '     123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'

 

2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。

例如 :

 

复制代码 代码如下:

>>> a = '123abc'
>>> a.strip('21')
'3abc'   结果是一样的
>>> a.strip('12')
'3abc'

Python Split函数的用法总结(



字符串的split用法

说明:
Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!
这里这样写的原因只是为了方便理解,仅此而已。

1.按某一个字符分割,如‘.’

1 str = ('www.google.com')
2 print str
3 str_split = str.split('.')
4 print str_split

结果如下:



2.按某一个字符分割,且分割n次。如按‘.’分割1次


1 str = ('www.google.com')
2 print str
3 str_split = str.split('.'1)
4 print str_split
结果如下:


3.按某一字符串分割。如:‘||’


1 str = ('WinXP||Win7||Win8||Win8.1')
2 print str
3 str_split = str.split('||')
4 print str_split

结果如下:


4.按某一字符串分割,且分割n次。如:按‘||’分割2次


1 str = ('WinXP||Win7||Win8||Win8.1')
2 print str
3 str_split = str.split('||',2)
4 print str_split
结果如下:


5.按某一字符(或字符串)分割,且分割n次,并将分割的完成的字符串(或字符)赋给新的(n+1)个变量。(注:见开头说明)
如:按‘.’分割字符,且分割1次,并将分割后的字符串赋给2个变量str1,str2


1 url = ('www.google.com')
2 str1, str2 = url.split('.'1)
3 print str1
4 print str2
结果如下:
缺图

一个正则匹配的例子:

>>> str="xxxxxxxxxxxx5 [50,0,50]>,xxxxxxxxxx"

>>> lst = str.split("[")[1].split("]")[0].split(",")

>>> print lst

['50', '0', '50']

分解如下

>>> list =str.split("[") 按照左边分割

>>> print list

['xxxxxxxxxxxx5 ''50,0,50]>,xxxxxxxxxx']

>>> list =str.split("[")[1].split("]")  包含的再按右边分割

再对所要的字符串按照分割副  存放在列表中

>>> list

['50,0,50', '>,xxxxxxxxxx']


>>> str.split("[")[1].split("]")[0]

'50,0,50'

>>> str.split("[")[1].split("]")[0].split(",")

['50', '0', '50']

>>> 



一个例子:判断输入的字符串是否为合法的IP

<img src="http://s8.sinaimg.cn/mw690/002ZKymTgy6JpBpoxYH27&690" width="690" height="389" name="image_operate_99831401914041285" alt="python strip() split()函数 介绍" title="python strip() split()函数 介绍" style="margin:0px; padding:0px; border:0px; list-style:none">

可以设计测试用例如下:

参考lhttp://www.51testing.com/html/55/n-212855.html

[root@akang python_practice]# python check_ip.py asdf

check ip address failed!

[root@akang python_practice]# python check_ip.py 10.12

check ip address failed!

[root@akang python_practice]# python check_ip.py !@#

-bash: !@#: event not found

[root@akang python_practice]# python check_ip.py a.s.d.f

check ip address failed!

[root@akang python_practice]# python check_ip.py 172.10.10.10

check ip address success!

[root@akang python_practice]# python check_ip.py 255.255.255.255

check ip address success!

[root@akang python_pract

### Python 中 `split` 和 `strip` 函数的使用方法 #### `strip()` 函数详解 `strip()` 方法用于移除字符串头尾指定的字符(默认为空白符,包括空格、制表符 `\t`、换行符 `\n` 等)。此方法不会修改原字符串而是返回一个新的字符串。 - **去除字符串两端的空格** 当不传递任何参数给 `strip()` 时,默认会删除字符串开头和结尾处所有的空白字符[^1]。 ```python text = " hello world! " cleaned_text = text.strip() print(cleaned_text) # 输出:"hello world!" ``` - **去除字符串两端指定的字符** 可以向 `strip()` 提供一个包含要删除字符的字符串作为参数。这些字符将从字符串的两侧被逐一尝试匹配并移除,直到遇到第一个不在该集合中的字符为止。 ```python text_with_chars = "...hello///world!!!" cleaned_text = text_with_chars.strip('.!/ ') print(cleaned_text) # 输出:"hello///world" ``` #### `split()` 函数详解 `split()` 是一种非常实用的方法,它按照指定分隔符来拆分字符串,并返回由各个子串组成的列表。如果不提供分隔符,则默认按任意数量的连续空白字符进行分割[^2]。 - **基本用法:按单个字符分割** 最简单的形式就是仅传入想要用来做为分界的单一字符。 ```python url = "www.google.com" parts = url.split('.') print(parts) # 输出:['www', 'google', 'com'] ``` - **控制分割次数:通过 maxsplit 参数** 还可以设置可选的最大切片数 `maxsplit` 来限制最终得到多少部分;一旦达到这个数目之后剩余的部分将会保持不变作为一个单独项存在于结果数组里。 ```python sentence = "one.two.three.four" limited_split = sentence.split('.', maxsplit=2) print(limited_split) # 输出:['one', 'two', 'three.four'] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值