Yoss
Yoss

Reputation: 21

How to use a variable in reading a filepath?

I'm trying to simplify the code instead of changing a bunch of variables.

For example:

title = "example"
filepath = r"C:\Users\User\Desktop\Research\title"

I want the output to be C:\Users\User\Desktop\Research\example, but it returns the quoted string.

How would I make it read the "title" variable instead?

Upvotes: 1

Views: 28

Answers (1)

rdas
rdas

Reputation: 21305

Use a formatting string along with your raw string.

title = "example"
filepath = rf"C:\Users\User\Desktop\Research\{title}"

Output:

>>> print(filepath)
C:\Users\User\Desktop\Research\example
>>> 

Upvotes: 1

Related Questions