Taylor_1989
- 400
- 14
The index slice is being used to read the year and month and day from the string as u mentioned. I am now slightly confused to how my slicing produces the desired result for the year as you are right the index [0:3] should read the first four characters of the string '2018' however when I run the index from [0:3] it printed:Mark44 said:Does the notation a[0:4] represent the characters in the string from index 0 through index 4? If so, that's five characters. And similarly in a[6:8], which I believe would be the three characters at indexes 6, 7, and 8.
I believe this is what you want:
year = (int(a[0:3]))
month = (int((a[4:5])))
day = (int(a[6:7]))
Python:
# Input date from the user/ for now just string in put
a = '20180101'# Define variable for the year, month and day and converting integer values
year = (int(a[0:3]))
print(year)
print('')
y = (int(a[0:4]))
print(y)
[CODE title="Output"]201
2018[/CODE]
So now my question is why is it reading in 5 characters for 2018 and not 4?
Last edited by a moderator: