簡體   English   中英

使用strip()從Python中的字符串中刪除字符

[英]Removing a character from a string in Python using strip()

我有兩個琴弦。

dat: "13/08/08
tim: 12:05:51+22"

我想從兩個字符串中刪除“字符。這是我正在使用的代碼:

dat=dat.strip('"')
tim=tim.strip('"')

我得到的結果字符串是:

dat: 13/08/08
tim: 12:05:51+22"

為什么沒有從Tim中刪除“”字符?

根據此處的文檔( http://www.tutorialspoint.com/python/string_strip.htm ),它應該可以工作。

根據文檔strip([chars])

返回刪除前導和尾隨字符的字符串的副本。 chars參數是一個字符串,指定要刪除的字符集。

因此, "不會從dat: "13/08/08替換dat: "13/08/08 ,而將從tim: 12:05:51+22"替換,因為"位於末尾:

>>> dat = 'dat: "13/08/08'
>>> tim = 'tim: 12:05:51+22"'
>>> dat.strip('"')
'dat: "13/08/08'
>>> tim.strip('"')
'tim: 12:05:51+22'

使用replace()代替:

>>> dat.replace('"', '')
'dat: 13/08/08'
>>> tim.replace('"', '')
'tim: 12:05:51+22'

似乎在這里工作

>>> tim2 = "tim: 12:05:51+22\""
>>> print tim2
tim: 12:05:51+22"
>>> tim = tim2.strip('"')
>>> print tim
tim: 12:05:51+22

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM