Skip to content

Slicing in Python

Advertisements

Slicing from a list or string can be achieved in Python using the following syntax.

Syntax

list[start:end:step]

Examples

nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print nums[5:8]
#[5, 6, 7]

Slicing from string can be done like..

url = 'https//poopcode.com'
print url[7:-4]
#poopcode

See also  Add n days to today's date in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.