1

This is my code:

f_name = input('First name?')
l_name = input('Last name?')
print(f_name, l_name)

I want to write the answers with quotes around the variables (so f_name, l_name). The answer my professor gave me is this:

print ("\"" + first_name + "\"" + "\t\"" + last_name + "\"") 

What I do not quite get here is why he uses the '+' and why he uses so many quotation marks. Can somebody help me?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Just to note, your professor's solution is quite suboptimal unless they were trying to not introduce any new topics. `print(f'"{first_name}"\t"{last_name}"')` is much nicer and doesn't require escaping quotes or manual string concatenation. – Carcigenicate Dec 19 '20 at 13:20

4 Answers4

1

+ means adding to objects together, in this case strings, those \s are used to escape the "s, becauseif you don't use \, and do:

print(""")

There will be an error, so you need those \s, but I think there are better ways to do what you want, like this:

print('"%"\t"%"' % (f_name, l_name))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Thank you for your quick answer! What I still do not quite get is why there are two "" after the \ and why I need so many + – Stefanie Fandl Dec 19 '20 at 13:21
  • @StefanieFandl There's two quotes after the slash because the first is "part of the slash". `\"` is treated as a single character. The slash is so Python knows that you want the quote inside the string, and not as an actual quote that the languages uses to indicate strings starting/ending. The second quote is closing the string literal. – Carcigenicate Dec 19 '20 at 13:24
  • @U11-Forward, IMHO it is not good to support the old style of formatting by using it in answers. – MarianD Dec 19 '20 at 13:33
1

A recommended way of doing this is to use the f-string or the format() method.

f_name = 'Jon'
l_name = 'Smith'

print(f'"{f_name}", "{l_name}"')
print('"{f_name}", "{l_name}"'.format(f_name=f_name, l_name=l_name))
Kheng
  • 114
  • 6
0

So what you need to understand is that the backslash '\' is an special character, often referred to as an 'escape' character. Which means that the character after the backslash is interpreted in an other way.

Because you want your first and second name to be within quotes you would have to use an escape character because the function print("") makes use of quotes. So when you would not use the backslash Python will think you end the string there.

You can find a list of all Python escape characters here: https://linuxconfig.org/list-of-python-escape-sequence-characters-with-examples

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thank you for your quick answer! What I still do not quite get is why there are two "" after the \ and why I need so many + – Stefanie Fandl Dec 19 '20 at 13:23
  • Okay, so to help you understand this I will break the function `print ("\"" + first_name + "\"" + "\t\"" + last_name + "\"") ` into pieces: 1. The first string `"\""` consists out of just a double quote (") 2. The second string `first_name` is the value of that paramterer. 3. The third string `"\""` is again a double quote (") And so on... You connect these string by making use of the character '+'. – Pieter Pauwels Dec 19 '20 at 13:26
0
f_name = input('First name?')
l_name = input('Last name?')
print("{}".format(f_name),"{}".format(l_name))
  • 3
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Dec 19 '20 at 15:27