|
| 1 | +#Demostration of Pythong String Indexing |
| 2 | + |
| 3 | +example= "Python Scripting" |
| 4 | + |
| 5 | +""" |
| 6 | +P Y T H O N S C R I P T I N G |
| 7 | +0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (Sequecebe ) |
| 8 | +-16-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 () |
| 9 | + """ |
| 10 | + |
| 11 | +print("Initial String: ") |
| 12 | +print(example) |
| 13 | + |
| 14 | +# Printing First character |
| 15 | +print("\nFirst character of String is: ") |
| 16 | +print(example[0]) |
| 17 | +#output would be P |
| 18 | + |
| 19 | +print ("\n7th Character of String is:\n",example[7]) |
| 20 | +#this will print only S |
| 21 | + |
| 22 | +# Printing Last character |
| 23 | +print("\nLast character of String is: ") |
| 24 | +print(example[-1]) |
| 25 | +#output would be G |
| 26 | + |
| 27 | +# Printing 3rd to 12th character |
| 28 | +print("\nSlicing characters from 3-12: ") |
| 29 | +print(example[3:12]) |
| 30 | +#output would be "hon Script" |
| 31 | + |
| 32 | +# Printing characters between |
| 33 | +# 3rd and 2nd last character |
| 34 | +print("\nSlicing characters between " + "3rd and 2nd last character: ") |
| 35 | +print(example[3:-2]) |
| 36 | +#hon Script |
| 37 | + |
| 38 | +#printing character from 1 to end |
| 39 | +print ("\nCharacter from 1 to :") |
| 40 | +print (example[1:]) |
| 41 | + |
| 42 | +#printing character from 1 to 6 |
| 43 | +print ("\nCharacter from 1 to 6") |
| 44 | +print (example[1:6]) |
| 45 | + |
| 46 | +#printing character from 1 to 12 |
| 47 | +print ("\nCharacter from 1 to 12") |
| 48 | +print (example[1:12]) |
| 49 | + |
| 50 | +#printing character from 1 to 12 with increment by 2 |
| 51 | +print ("\nCharacter from 1 to 12 with increment by 2") |
| 52 | +print (example[1:12:2]) |
| 53 | + |
| 54 | + |
| 55 | +#printing character from 1 to 12 with increment by 2 |
| 56 | +print ("\nCharacter from -3 to -1 with increment by 2") |
| 57 | +print (example[-9:-1:-1]) |
| 58 | + |
| 59 | +#printing character from -3 -15 in reverse order |
| 60 | +print ("\nCharacter from -3 -15 in reverse order") |
| 61 | +print (example[-3::-1]) |
0 commit comments