简体   繁体   中英

Formatting strings with integers

I'm trying to increment the video file names every time they get into my folder. I tried the + and the join() method but I can't seem to figure it out. I tried integers without quotation marks but the join method wont let me use an integer so I tried with quotation marks but now it won't increment

Here is my code

 VideoNumber += "99"
 folderLocation = ("C:/Users/someone/Documents", VideoNumber, ".mp4")
 x = "/".join(folderLocation)
 print(x)

You can format integers into a string using an f-string or the format() method on strings.

video_number += 99
video_path = f"C:/Users/someone/Documents/{video_number}.mp4"
print(video_path)

Just as an example of how to make your original code work, you could keep your number as an integer and then convert it to a string using str() (though note this has a bug because you will have an extra / between the number and .mp4 ).

VideoNumber += 99
folderLocation = ("C:/Users/someone/Documents", str(VideoNumber), ".mp4")
x = "/".join(folderLocation)
print(x)

您可以将整数转换为字符串,因此您的代码将是这样的

folderLocation = ("C:/Users/someone/Documents", str(VideoNumber), ".mp4")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM