How to get substring from a string using slice object in Python

2 Answers

0 votes
s = 'Python'

so = slice(4)

print(s[so])

so = slice(0, 5, 2)

print(s[so])


'''
run:

Pyth
Pto

'''

 



answered Dec 16, 2018 by avibootz
0 votes
s = 'Python'

# slice(start, stop, step)
so = slice(-1, -6, -2)

print(s[so])


'''
run:

nhy

'''

 



answered Dec 16, 2018 by avibootz
...