Iterating over a specific range in a python string -
im having difficulty figuring out how iterate on string in python. goal iterate point in string, , remove rest of elements after iteration point , work substring.
lets have string, str1:str1 = "8493 2020"
, have number specifies how far want iterate, a = 4
how can iterate on str1 - , remove characters after a?
i tried got syntax error of "typeerror: 'str' object not callable":
for in str1(range(a)): print
you iterate on slice.
>>> str1 = "8493 2020" >>> = 4 >>> in str1[:a]: print 8 4 9 3
if need create new string, do.
>>> b = str1[:a] >>> b '8493'
Comments
Post a Comment