Home > Back-end >  running sql code in python difference between r''' and f'''
running sql code in python difference between r''' and f'''

Time:01-10

I have a code like below. I noticed that i can replace f''' with r''' what is the difference between those 2 options and when to use it?. it seems that r''' when there is regex in the code? i tried to google but didnt get any good results

query = f'''

with a as (
select 
some sql or hive code

from a
'''

CodePudding user response:

f"" strings are strings that can be formatted. How it works: you specify the code in curly braces and the result of this code will be substituted into your string.

print(f"2   2 = {2   2}")  # 2   2 = 4
print(f"max = {max(7, 8)}")  # max = 8

Notes:

  1. You won't be able to reuse the same kind of quotes as you used to designate the main string.

    print(f"Hello, {"world!"}") # SyntaxError
    print(f"Hello, {'world!'}") # Hello, world!

  2. If you need curly brackets without formatting, add another one of the same bracket.

    print(f"Example {{ text }}") # Example { text }

  3. You cannot use a backslash when formatting.

    print(f"No backslash {'\n'}") # SyntaxError

r"" string is a raw string. The interpreter will treat the backslash as a regular character

print("Hello,\tworld!")  # Hello,   world!
print(r"Hello,\tworld!")  # Hello,\tworld!
  • Related