结合f字符串和原始字符串文字 [英] Combine f-string and raw string literal

查看:94
本文介绍了结合f字符串和原始字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在使用r来获取原始字符串文字的同时使用f字符串.我目前如下所示,但希望选择允许任何名称替换Alex的选项,我当时正在考虑添加f字符串,然后用花括号替换Alex并将用户名放入其中,但这不适用于r.

I'm wondering how to use an f-string whilst using r to get a raw string literal. I currently have it as below but would like the option of allowing any name to replace Alex I was thinking adding an f-string and then replacing Alex with curly braces and putting username inside but this doesn't work with the r.

username = input('Enter name')
download_folder = r'C:\Users\Alex\Downloads'

推荐答案

您可以将f字符串的f与文字的r组合.

You can combine the f for an f-string with the r for a literal.

user = 'Alex'
dirToSee = fr'C:\Users\{user}\Downloads'
print (dirToSee) # prints C:\Users\Alex\Downloads

这篇关于结合f字符串和原始字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆