39. Advanced String Handling Pt. 3

  • You can chop up strings and check individual parts when the input may be multiple words or numbers separated by spaces or particular characters such as full names or dates
  • In the last example you split strings into an array but you can access individual bits of them by number to save time
  • Remember to use if statements to validate input!
#Date variable
date = input(“Enter the date (dd/mm/yyyy): ”)

#Access the string directly
day = date[0:2]
month = date[3:5]
year = date [8:12]

#You can't change individual bits of the string though
#date[0:2] = "15"

#You might recreate the original instead after updates
day = "15"
date = day + "/" + month + "/" + year

#Print new date string
print(date)

Tasks

  1. Type in the code and test it works
  2. Save and label your code