本文介绍了从字符串中删除前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图从二进制数的左端去除0b1".
以下代码导致剥离所有二进制对象.(不好)
>>>bbn = '0b1000101110100010111010001' #转换后的 bin(2**24+**2^24/11)>>>aan=bbn.lstrip("0b1") #尝试一次剥离所有左端垃圾.>>>打印 aan #oops 都消失了.''所以我分两步完成了 .lstrip():
>>>bbn = '0b1000101110100010111010001' # 同分数表达式>>>aan=bbn.lstrip("0b")# 以前做过这个.>>>打印 aan #Extra "1" 仍然存在.'1000101110100010111010001'>>>aan=aan.lstrip("1")# 如果一开始没有成功...>>>打印 aan #YES!'000101110100010111010001'这是怎么回事?
再次感谢您通过一个简单的步骤解决了这个问题.(见我之前的问题)
解决方案
strip 系列将 arg 视为要删除的 set 字符.默认设置为所有空白字符".
你想要:
if strg.startswith("0b1"):strg = strg[3:]
Trying to strip the "0b1" from the left end of a binary number.
The following code results in stripping all of binary object. (not good)
>>> bbn = '0b1000101110100010111010001' #converted bin(2**24+**2^24/11)
>>> aan=bbn.lstrip("0b1") #Try stripping all left-end junk at once.
>>> print aan #oops all gone.
''
So I did the .lstrip() in two steps:
>>> bbn = '0b1000101110100010111010001' # Same fraction expqansion
>>> aan=bbn.lstrip("0b")# Had done this before.
>>> print aan #Extra "1" still there.
'1000101110100010111010001'
>>> aan=aan.lstrip("1")# If at first you don't succeed...
>>> print aan #YES!
'000101110100010111010001'
What's the deal?
Thanks again for solving this in one simple step. (see my previous question)
解决方案
The strip family treat the arg as a set of characters to be removed. The default set is "all whitespace characters".
You want:
if strg.startswith("0b1"):
strg = strg[3:]
这篇关于从字符串中删除前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!