# Slicings.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-24 File created. # 2022-12-27 Converted to Python 3. # This progrm demonstrates how to take "slices" of a string. Slices are # mostly substrings. Slices can be taken in the same way from lists and # tuples. # Slicing means using several indexes in brackets. # Indexing is 0-based. # Negative indexes (usually) mean counting backwards from end of a string. # Indexes are written inside brackets in the following way # [ starting_at_index : less_than_index : step ] # where # starting_at_index defaults to 0, # less_tan_index defaults to the length of the string, # and step defaults to 1. test_string = "ABCDEFGHIJKLMN" print( " " + "012345678901234567890" ) print( "test_string " + test_string ) print( "test_string[ 3 ] " + test_string[ 3 ] ) print( "test_string[ - 1 ] " + test_string[ - 1 ] ) print( "test_string[ 2 : 6 ] " + test_string[ 2 : 6 ] ) print( "test_string[ 3 : len( test_string ) ] " + test_string[ 3 : len( test_string ) ] ) print( "test_string[ 3 : ] " + test_string[ 3 : ] ) print( "test_string[ 0 : 6 ] " + test_string[ 0 : 6 ] ) print( "test_string[ : 6 ] " + test_string[ : 6 ] ) print( "test_string[ : ] " + test_string[ : ] ) print( "test_string[ : : 2 ] " + test_string[ : : 2 ] ) print( "test_string[ 1 : -1 ] " + test_string[ 1 : -1 ] ) print( "test_string[ : : -1 ] " + test_string[ : : -1 ] )