Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

slicing string in python

# The first number starts counting from '0'
# Always remember that second number in the square bracket says
# "up to but not including"
s = 'Monty Python'
print(s[0:4])   # Output: Mont
print(s[6:7])   # Output: P
print(s[6:20])  # It doesn't give a Traceback eventhough there are no 19 letters
# Output: Python
# If don't demarcate the first or second number it will go till the start or end accordingly
print(s[:2])	# Output: Mo
print(s[8:])	# Output: thon
print(s[:])		# Output: Monty Python
Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #slicing #string #python
ADD COMMENT
Topic
Name
9+7 =