[英]How can I get 2 characters from the middle of a string?
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)? 我怎样(最好)使用[: number :]的东西来寻找“18:05:14”(05)的第4和第5个角色? if i can't use [:#], that's fine. 如果我不能使用[:#],那没关系。 I can't find any possible way, but any help is appreciated. 我找不到任何可能的方式,但任何帮助表示赞赏。
Specify a start and stop position: 指定开始和停止位置:
>>> date = '18:05:14'
>>> date[3:5]
'05'
>>>
[3:5]
will get every character from index 3
inclusive to index 5
exclusive. [3:5]
将从索引3
包含到索引5
每个字符独占。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.