python - Excluding a specific string of characters in a str()-function -
a small issue i've encountered during coding.
i'm looking print out name of .txt file. example, file named: verdata_florida.txt, or verdata_newyork.txt how can exclude .txt , verdata_, keep string between? must work number of characters, .txt , verdata_ must excluded.
this far, i've defined filename input()
print("average tam at", str(filename[8:**????**]), "is higher ")
you can leverage str.split() on strings. example:
s = 'verdata_newyork.txt' s.split('verdata_') # ['', 'florida.txt'] s.split('verdata_')[1] # 'florida.txt' s.split('verdata_')[1].split('.txt') ['florida', ''] s.split('verdata_')[1].split('.txt')[0] # 'florida'
Comments
Post a Comment