在Python中,如何分割字符串?

问题描述 投票:-3回答:1
mystring = '[:3]'
x = mystring.split()
z = str(x).lstrip("['").rstrip("]'")
print(z)
print('hello'+int(z))

错误:ValueError:以10为底的int()的无效文字:':3'

如何通过对'mystring'变量进行切片来使输出为'hel'?

python python-3.x string slice
1个回答
0
投票

我认为这是您要尝试做的....

mystring = '[:3]'
#x = mystring.split()
z = str(mystring).lstrip("['").rstrip("]'")
print(z)
print('hello'+z)

所以输出是..

:3
hello:3
© www.soinside.com 2019 - 2024. All rights reserved.