Python Forum
[SOLVED] Print an int with a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Print an int with a string
#1
Is there a way to print an int (score) inside of a string without getting an error?
score = 0
score = score + 1
print("Your score was ")
print(score)
print(" out of 5")
That is the code i currently have, which prints
Output:
Your score was 1 out of 5
And when i change it to this
score = 0
score = score + 1
print("Your score was " + score + " out of 5")
I get the error
Error:
Traceback (most recent call last): File "C:\Users\{USER}\Desktop\Python\game.py", line 115, in <module> print("Congratulations! You scored " + score + " out of 5 points") TypeError: must be str, not int
Is there anyway to fix this? I thought about changing the score to a string in the beginning
score = "0"
But that wouldn't work because when you get a question right, and use
score = score + 1
You would get an error. Unless you use
score = score + "1"
But then score would equal to "01" and not "1"
Self-taught HTML, CSS, Python, and Java programmer
Reply
#2
Please read the docs about strings... specially the format and the f-strings.
As a summary there are 4 methods to create strings with the value of a variable in it:
  1. Using str:
    print("Your score was " + str(score) + " out of 5")
    You have no control of the format used
  2. Using the old % syntax:
    print("Your score was %d out of 5" % score)
    # Or, for more than 1 variable
    print("Your score was %d out of %d" % (score, 5))
    You control the format and is easy for 1 or 2 variables...
  3. The format command:
    print("Your score was {} out of 5".format(score))
    # but can be something complex as:
    print("Your score was {:{w}d} out of {}".format(score, 5, w=3))
    Much versatile than the %-formatting, more complex, also.
  4. f-strings, only for python3.6+
    print(f"Your score was {score} out of 5")
    # Accept almost anything you can do with format
    w = 3
    print(f"Your score was {score:{w}d} out of 5")
    For me the cleanest option to format strings...

There are a few more (strings.Template) but with this 4 options you cover the 99% of the cases.
Reply
#3
>>> score = 0
>>> score += 1
>>> print("Your score was {} out of 5".format(score))
Your score was 1 out of 5
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,509 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,114 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Remove a space between a string and variable in print sie 5 1,780 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,248 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Can you print a string variable to printer hammer 2 1,941 Apr-30-2022, 11:48 PM
Last Post: hammer
  [Solved] How to print a dot leader. BashBedlam 4 1,623 Jan-02-2022, 04:28 PM
Last Post: BashBedlam
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,203 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Print first day of the week as string in date format MyerzzD 2 2,011 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,098 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 5,008 Aug-07-2021, 08:16 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020