Other ways to embed variable values into raw strings: ```python import re c = 5 s = "this and that alrighty to booth" re.findall(r'\w{' + str(c) + ',}', s) ['alrighty', 'booth'] re.findall(r'\w{%s,}' % c, s) ['alrighty', 'booth'] re.findall(r'\w{{{},}}'.format(c), s) ['alrighty', 'booth'] re.findall(r'\w{{{c},}}'.format(c=c), s) ['alrighty', 'booth'] re.findall(fr'\w{{{c},}}', s) ['alrighty', 'booth'] ``` Post back if you need more help. Good luck!! Answer from Chris Freeman on teamtreehouse.com
Discussions

Regex Search raw string on variable?
One point - re.search won't interpret my_variable as a raw_string, the Python interpreter will. The r'abcde' notation tells the interpreter "if you see an escape character, I want the actual escape character, not what it means." For example: x = r'\tabc' print(x) '\tabc' y = '\tabc' print(y) '_________abc' (a tab before abc) We often use the raw string notation with regex because the regex needs to contain the escape characters, and we don't want to start writing lots of double backslashes. So in this case, you don't need to worry about my_variable being treated as a raw string by re.search - it's already wanting a string as the input, and using the x=r'abc' is a way to avoid double backslashes. Since re.search just wants a string for the first part, you can concatenate these strings as you would any others. searchRegex = re.search(my_variable + r'[&\n\s]*', current_line) More on reddit.com
🌐 r/learnpython
6
1
July 30, 2015
how to turn a string argument into raw string
You might find the below helpful: https://realpython.com/python-pathlib/ More on reddit.com
🌐 r/learnpython
3
2
May 18, 2021
python - How do I put a variable’s value inside a string (interpolate it into the string)? - Stack Overflow
I would like to put an int into a string. This is what I am doing at the moment: num = 40 plot.savefig('hanning40.pdf') #problem line I have to run the program for several different numbers, so I'... More on stackoverflow.com
🌐 stackoverflow.com
How to turn a BeautifulSoup object into a String?
just assign it to a new variable. soup = BeautifulSoup(url) soup_string = str(soup) Edit - I see you mentioned what you need is inside an li tag. You'll need to use thing = soup.find("li", "#search string") and then make thing_string = str(thing) or some variation on that. I used something similar to grab a webcomic string path on a scraper I made recently here if you want an idea of how to parse that with re. The relevant info is in the comic_soup() and get_comics() functions. It's likely you'll need to parse it with regex, depending on what you're trying to grab, as when you convert to string, it will grab the entire tag
  • More on reddit.com
    🌐 r/Python
    20
    6
    August 26, 2014
    🌐
    Data Science for Everyone
    matthew-brett.github.io › teaching › string_formatting.html
    Inserting values into strings — Tutorials on imaging, computing and mathematics
    If you can depend on having Python >= version 3.6, then you have another attractive option, which is to use the new formatted string literal (f-string) syntax to insert variable values. An f at the beginning of the string tells Python to allow any currently valid variable names as variable ...
    🌐
    Python
    mail.python.org › pipermail › tutor › 2013-May › 095244.html
    [Tutor] Raw string form variable
    May 5, 2013 - So we have: py> a = r'C:\win\apple.exe' # Use raw string syntax py> b = 'C:\\win\\apple.exe' # Use normal string syntax, with escaped backslashes py> print a, b, a == b C:\win\apple.exe C:\win\apple.exe True When you do this: path = 'c:\win\apple.exe' you are not using a raw string, so backslashes have special meaning. Backslash-w has no special meaning and is left as given, but backslash-a means the ASCII BEL character. If you read path from a file, then backslashes are just backslashes, and nothing special happens. It is only when you type a literal string in Python source code that backslas
    🌐
    GeeksforGeeks
    geeksforgeeks.org › python › insert-a-variable-into-a-string-python
    Insert a Variable into a String - Python - GeeksforGeeks
    July 23, 2025 - This is Python programming! Explanation: In an f-string, the variable a is inserted directly by placing it inside {} within a string prefixed with f, enabling dynamic content without manual concatenation.
    🌐
    Built In
    builtin.com › articles › python-variable-in-string
    How to Insert a Python Variable in a String | Built In
    There are four common methods that will allow you to add Python variables in a string, including: the comma, the modulus operator, string format and f-strings. Here’s how they work. ... Summary: Python offers multiple ways to insert variables into strings, including comma-separated printing, % formatting, .format() and f-strings.
    🌐
    Reddit
    reddit.com › r/learnpython › regex search raw string on variable?
    r/learnpython on Reddit: Regex Search raw string on variable?
    July 30, 2015 -

    I need to perform a regex search that includes a variable that may contain regex characters in it. I'd like to have the re.search interpret the variable as a raw string but can't seem to find the proper format.

    Example:

    my_variable = "[id-1\s]["
    
    searchRegex = re.search(my_variable+[^&\n\s]*", current_line)

    Since I can't quote out my_variable, how can I ensure re.search interprets it as a raw string?

    UPDATE: I ended up using re.escape() to solve the problem per u/zahlman

    my_variable = "[id-1\s]["
    
    searchRegex = re.search(re.escape(my_variable)+[^&\n\s]*", current_line)
    🌐
    Flexiple
    flexiple.com › python › insert-variable-string-python
    How to Insert a Variable into a String in Python - Flexiple
    February 4, 2024 - It uses the % operator to insert variables into strings, similar to C's printf() function. name = "David" age = 28 print("My name is %s and I am %d years old." % (name, age)) Each string formatting technique has its advantages and use cases.
    Find elsewhere
    🌐
    AskPython
    askpython.com › home › raw strings in python: a comprehensive guide
    Raw Strings in Python: A Comprehensive Guide - AskPython
    May 22, 2023 - We can easily pass the value into a variable, by considering it as a raw string literal! ... Now, there is no problem, and we can pass this raw string literal as a normal object! ... NOTE: In some cases, if you’re printing a Python raw string on the console, you may get something like this:
    🌐
    Reddit
    reddit.com › r/learnpython › how to turn a string argument into raw string
    r/learnpython on Reddit: how to turn a string argument into raw string
    May 18, 2021 -

    Hi I am working on my project which involves retrieving image and folder paths so I can display them.

    I saw that when putting in paths you need to replace \ with \\ due to python syntax

    so I wrote this function to try and do that

    #returns a string replacing the \ with double \\ To operate on
    def slashreturn(string):
        string2 = r'{}'.format(string)
        ustring = string2.replace("\\","/")
        print(ustring)
        post = ""
        for i in ustring:
            if i == "/":
                post += str(i*2)
            else:
                post += i
        return print(post.replace("/","\\"))

    I quickly came up against the (SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape) Error. Researching this I found that turning the input into raw string would solve the issue and it does however I need the process to be automated. Thats why I tried doing the

      string2 = r'{}'.format(string)

    But still no luck. I still get the error.

    I would really appreciate if anyone could help me out with how to do this

    cheers!

    🌐
    Python Forum
    python-forum.io › thread-38555.html
    python r string for variable
    October 28, 2022 - Hi Team, how to apply r(raw string) for a variable path r"\\IND200300400.XXX.XXXX.XXX\Recon Project\output1" ---------here directly attached r its ok. folderpath = \\IND200300400.XXX.XXXX.XXX\ path =
    🌐
    EDUCBA
    educba.com › home › software development › software development tutorials › python tutorial › python raw string
    Python Raw String | Learn How raw strings used in Python?
    April 4, 2023 - When we use the raw string, the output will be as shown in the below Example. ... Suppose we want to print a String (‘Today is \n Thursday’). First, assign the string to a variable.
    Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
    🌐
    DigitalOcean
    digitalocean.com › community › tutorials › python-raw-string
    How To Use Python Raw String | DigitalOcean
    March 3, 2026 - To combine raw string behavior with variables, use an rf"" or fr"" string (available in Python 3.6 and later).
    🌐
    Quora
    quora.com › How-do-I-insert-a-variable-into-a-string-in-Python
    How to insert a variable into a string in Python - Quora
    Answer (1 of 4): Method #1: using String concatenation In the first method, we'll concentrate a variable with a string by using +. Let's see the following example. [code] # variable variable = "Python" # insert a variable into string using String ...
    🌐
    GeeksforGeeks
    geeksforgeeks.org › python-raw-strings
    Python Raw Strings - GeeksforGeeks
    In this example, we defined a string, which is basically a file path of Windows, using r before quotes and assigned it to a variable. Further, we have printed that variable to see the result. ... # Python program to use raw strings # Define string with r before quotes # and assign to variable s1 = r'c:\parent\tasks\new' print(s1)
    Published   April 28, 2025
    🌐
    Plain English
    python.plainenglish.io › how-to-use-python-raw-string-literals-to-get-more-readable-code-3ed301cad439
    How to Use Python Raw String Literals to Get More Readable Code | by Sébastien Combéfis | Python in Plain English
    July 23, 2021 - Let’s look at the following example which shows how to define a raw string literal, compared to the definition of a “normal” string literal: s1 = 'Hello\nWorld' print('s1:', s1)s2 = r'Hello\nWorld' print('s2:', s2) In the first line, a “normal” string literal is used to initialise the s1 variable.
    🌐
    iO Flood
    ioflood.com › blog › python-string-interpolation
    Python String Interpolation | Learn 3 Efficient Methods
    July 5, 2024 - Master Python String Interpolation! Discover how to insert variables directly into strings with `f-strings` , `str.format()`, and the `%`Operator.
    🌐
    PyTutorial
    pytutorial.com › python-variable-in-string
    PyTutorial | Python: Add Variable to String & Print Using 4 Methods
    September 5, 2023 - # Variables name = "Pytutorial" age = 22 # Insert Variables into String greeting = "Hello, %s! You are %s years old" % (name, age) # Print Result print(greeting) ... The str.format() method in Python is used for string formatting and is an alternative to the older % operator for string interpolation.
    🌐
    Python Guides
    pythonguides.com › create-a-string-with-variables-in-python
    Ways to Insert a Python Variable into a String
    February 2, 2026 - You just need to add the letter f before your string and put your variables inside curly braces {}. Here is an example where I’m calculating the total cost of a tech gadget, including Texas sales tax: # Using f-strings to format a price product = "MacBook Pro" price = 1999.99 tax_rate = 0.0825 ...