0

I am currently doing a tutorial in repl.it, and have used this website a little bit for web development. I cannot understand how to do this problem, and I think I got everything else right, except that there is a whitespace in the middle of what Python 3 prints out.

The question is: Given a two-digit integer, swap its digits and print the result.

The code I have written is this:

a = int(input())
b = int(a / 10) 
c = int(a % 10)
print(c, b)

When I put the sample input in, 79, it seems to swap correctly, but it leaves a space, of which I know is technically a character and is wrong.

Input: 79

Output: 9 7

Thanks for answering!

Esmooth 39
  • 11
  • 3

1 Answers1

1

The Problem

The comma in the print statement tells python to put a space.

The Solution

You want to put both integers together as strings, so this is your best route:

a = int(input())
b = int(a / 10) 
c = int(a % 10)
print("{0}{1}".format(c, b))

Experimenting with the Code

I invite you to try some variations on the format string to understand best how it works.

ex1

print("{0} and {1}".format(7, 9))
# output: 7 and 9

Examine what happens when you change the characters inside the string, or in the format function.

ex2

print("My first name is {0} and {0} likes {1}!".format("Samy", "pie"))
# output: My first name is Samy and Samy likes pie!

I hoping it becomes clear that the format function preforms substitutions in strings.

"{0}" gets replaced with the first argument to the format call. "{1}" gets replaced with the first argument to the format call. and so on..

You can have additional arguments:

ex3

print("My favorite numbers are {0}, {1}, {2}, and {3}.".format(9, 23, 45, 97))
# output: My favorite numbers are 9, 23, 45, and 97.

More information

My favorite way to learn python was always trial and error, and experimenting with the code. It has always felt like the language wanted me to do that, but another great way to learn is to read specifications.

Visit the specification of the format function: https://docs.python.org/3.4/library/string.html#format-string-syntax

You will learn everything there is to know about the function.

Most notably,

The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order.

Notice that field_name refers to what's on the inside of {}

Samie Bencherif
  • 1,285
  • 12
  • 27
  • ^^^ Using `.format` is extremely unnecessary here. You can use the `sep` parameter in `print` to do this instead. – rayryeng Nov 19 '19 at 18:59
  • 2
    or, `f'{c}{b}'` using the new formatting option in more recent Python versions. – Jorge Leitao Nov 19 '19 at 18:59
  • May you explain, step by step, what this does? I am relatively new and I find it hard to understand all of the intricacies of what is going on here. – Esmooth 39 Nov 19 '19 at 19:01
  • @Esmooth39 Did you try the example? – Samie Bencherif Nov 19 '19 at 19:02
  • @SamyBencherif Yes, I did, however, I don't understand how it works. I'm happy that it does, but the whole point of me doing the tutorial is so I can understand how it works. I was just wondering if you could spell it out to me, since I don't understand what functions you used, how they work, etc. – Esmooth 39 Nov 19 '19 at 19:06
  • @Esmooth39 Ok I leave you with some more examples with output, a link to the reference, and a general meditation about the python experience. Please enjoy – Samie Bencherif Nov 19 '19 at 19:19