Some Notes as I brush back up on Python

Indexes and Slices with Python Strings

mystring = 'abcdefghijklmnopqrstuvwxyz'
#Check variable type
print(type(mystring))
#Find the length of the variable
print(len(mystring))
print(mystring)
#Slicing is [start:stop:step]
print(mystring[0])
print(mystring[-1])
#Print Characters 22 forward
print(mystring[22:])
#Print First 3 characters, note that a is 0 and d is 3
#but we are saying with the below to go up to the letter D
#but don't include it
print(mystring[:3])
#Print Characters d thru f
print(mystring[3:6])
#Print All Characters
print(mystring[::])
#Hop Scotch by 2's
print(mystring[::2])
#Subsection and then Hop Scotch by 2's
print(mystring[11:19:2])
#Finally let's roll in reverse
print(mystring[::-1])

Terminal @ TuxLabs

python strings.py

<class 'str'>
26
abcdefghijklmnopqrstuvwxyz
a
z
wxyz
abc
def
abcdefghijklmnopqrstuvwxyz
acegikmoqsuwy
lnpr
zyxwvutsrqponmlkjihgfedcba

  

Formatting with .format() method in Python

print('Hello World, this is GrandMasterTux')
print('Hello {}, this is GrandMasterTux'.format('World'))
print('Hello {}, this is {}'.format('World','GrandMasterTux'))
print('Hello {0}, this is {1}'.format('World','GrandMasterTux'))
print('Hello {1}, this is {0}'.format('World','GrandMasterTux'))
print('Hello {1}, this is {1}'.format('World','GrandMasterTux'))
print('Hello {w}, this is {g}'.format(w='World',g='GrandMasterTux'))

Terminal @ TuxLabs

python formatting.py

Hello World, this is GrandMasterTux
Hello World, this is GrandMasterTux
Hello World, this is GrandMasterTux
Hello World, this is GrandMasterTux
Hello GrandMasterTux, this is World
Hello GrandMasterTux, this is GrandMasterTux
Hello World, this is GrandMasterTux