0

I have a quick question.

I just want to print parts of an integer. For example, my integer is today's date: 20190327 ...and I would like to print the "03" part.

Is this even possible with integers?

I would like to do something similar to this:

todays_date = 20190327
print(todays_date[0:5])

But this doesn't work. Is there a simple solution? Thanks!

1
  • 3
    str(todays_date)[0:5]? Commented Mar 27, 2019 at 22:54

1 Answer 1

3

You're almost there:

todays_date = '20190327'
print(todays_date[0:5])

You just needed to ensure todays_date was a string. Putting quotes around this does that. Alternatively, if you would still like to use todays_date as an integer, you can convert it to a string just for the print statement:

todays_date = 20190327
print(str(todays_date)[0:5])
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.