How can I get 2 characters from the middle of a string?

38,925

You have many ways to get value.

print(date[3:5])           # 05 (start the 4th to 5th char)
print(date.split(":")[1])  # 05 (split string and get 2nd string)
Share:
38,925
Peaser
Author by

Peaser

Updated on May 19, 2020

Comments

  • Peaser
    Peaser about 4 years
    import time
    
    date = time.strftime("%d:%m:%y")
    
    
    print date #returns '18:05:14'
    
    print date[-2:] #returns '14'
    
    print date[:2] #returns '18'
    
    #print ??? <-returns '05'
    

    How can I (preferably) use the [:number:] thing to look for the 4th and 5th character ONLY of "18:05:14" (05)? if i can't use [:#], that's fine. I can't find any possible way, but any help is appreciated.

  • Peaser
    Peaser about 10 years
    You guys are awesome. Thank you!