在此失败示例中使用 string.lstrip() 的正确方法是什么?
What is the correct way to use string.lstrip() in this failing example?
此代码...
print( "PO #{}".format('PUR-ORD-2021-00001'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00002'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00003'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00008'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00018'.lstrip('PUR-ORD-2021-')))
...returns:
PO #
PO #
PO #3
PO #8
PO #8
我不是 Python 专家,但根据我目前的经验,Python 的结果总是非常合理。但是这个?完全看不懂
这是怎么回事?
版本:
me@mine:~$ python
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
您可以只替换部分字符串来获取订单号:
original_string = 'PUR-ORD-2021-00001'
order_number = original_string.replace('PUR-ORD-2021-', '')
print('PO #', order_number)
# PO # 00001
此代码...
print( "PO #{}".format('PUR-ORD-2021-00001'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00002'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00003'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00008'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00018'.lstrip('PUR-ORD-2021-')))
...returns:
PO #
PO #
PO #3
PO #8
PO #8
我不是 Python 专家,但根据我目前的经验,Python 的结果总是非常合理。但是这个?完全看不懂
这是怎么回事?
版本:
me@mine:~$ python
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
您可以只替换部分字符串来获取订单号:
original_string = 'PUR-ORD-2021-00001'
order_number = original_string.replace('PUR-ORD-2021-', '')
print('PO #', order_number)
# PO # 00001