Hello everyone I am of course new to ppython and I am having trouble joining an integer variable and a string without a space in between them. Lets say my variabe was an integer of num = 121 and I wanted to print the proper ending of "st". which would make it 121st how would I go about it? Here is my code it works fine accept for an unwanted space.

``````num = input("Please enter a number : ")
numin = int(num)
while (num > 100):
num = num - 100

if num == 1 or num > 20 and num % 10 == 1:
print  str(numin) ,("st")
elif num % 10 == 2 and num > 20 or num == 2:
print str(numin), ("nd")
elif num % 10 == 3 and num > 20 or num == 3:
print str(numin), ("rd")
elif num % 10 == 4 or num % 10 == 5 or num % 10 == 6 or num % 10 == 6 or num % 10 == 6 or num % 10 == 7 or num % 10 == 8 or num % 10 == 9 or num % 10 == 0 or num == 11 or num == 12 or num == 13:
print str(numin), ("th")
``````

Here is what my ouput looks like, Also if anyone see any syntax errors that I have made feel free to correct me. I don't want to develop bad habits. Thanks in advance
Please enter a number : 121
121 st

================================ RESTART ================================

Please enter a number : 145
145 th
================================ RESTART ================================

Please enter a number : 735
735 th
================================ RESTART ================================

Please enter a number : 32
32 nd
================================ RESTART ================================

Please enter a number : 33
33 rd

In your code, `num` is a string, and `numin` is an integer. It means that the arithmetic expressions involving `num` won't work as expected. The best thing to do is to replace line 2 with `num = int(num)` and forget about `numin`. You can even write `num = int(num) % 100`.

To answer your initial question, you can write `print "%dst" % 121`, or `print "{0}st".format(121)` or `print str(121) + "st"`. My preferred way is the format method.

The last `elif...` in your code could also be replaced by a single `else:`. It seems to me that `if (num % 10 == 1) and (num != 11)` would be easier for the reader that `num > 20`, etc. You could also have a variable `r = num % 10` and write `if (r == 1) and (num != 11)` etc.

Finally, here is an advanced solution

``````num = int(num) % 100
suffix = dict(enumerate(("st", "nd", "rd"), 1))
s = "th" if num in (11, 12, 13) else suffix.get(num % 10, "th")
print "{0}{1}".format(num, s)
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.