繁体   English   中英

用整数格式化字符串

[英]Formatting strings with integers

每次视频文件进入我的文件夹时,我都会尝试增加它们的名称。 我尝试了+join()方法,但我似乎无法弄清楚。 我尝试了不带引号的整数,但连接方法不允许我使用整数,所以我尝试使用引号,但现在它不会增加

这是我的代码

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

您可以使用f 字符串或字符串的format()方法将整数格式化为字符串。

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

作为如何使原始代码工作的示例,您可以将数字保留为整数,然后使用str()将其转换为字符串(但请注意,这有一个错误/因为您将在数字和.mp4 )。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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