Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to slice a string in python

# To slice a string, use square brackets to index the string. 
# Like a range, it includes the first number but doesn't include the second.
string = "I love Grepper"
# The index starts from 0 not 1
s = string[0:3]# Assigns the first three characters of str to s
s2 = string[0:]# Assigns all the characters in a string to s2
s3 = string[:4]# Assigns from 0 up to the 4th character to s3
s4 = string[1:6:2]# Allows you to specify a step as well
Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #slice #string #python
ADD COMMENT
Topic
Name
4+6 =