I have written some code to count the number of upper case letters in a string and the last line will not work?

I am trying to print a statement that concatenates a line of text and an integer variable:

print ("The number of upper case letters are: " + upper)

When I run the code it give me the following error:

Traceback (most recent call last):
File "C:\Users\Emma\Desktop\Test", line 36, in <module>
print ("The number of upper case letters are: " + upper)
TypeError: Can't convert 'int' object to str implicitly

Any help would be gratefully received.

Dani AI

Generated

The error comes from trying to join a text string and an integer without converting the integer first. correctly pointed out the root cause and listed the classic workarounds (convert to text, use formatting, or pass multiple print arguments). The following adds modern options, a compact way to count uppercase characters, and a few practical debugging tips.

Count uppercase letters and print the result cleanly (Python 3.6+ example using an f-string and a generator expression):

s = "Hello World!"
upper_count = sum(1 for ch in s if ch.isupper())
print(f"Uppercase letters: {upper_count}")

Quick checks and best practices

  • Prefer descriptive names (for example upper_count instead of upper) so code does not shadow string methods.
  • Verify types when debugging: use isinstance or inspect type()/repr() to confirm a value is actually an int. For example, raise a clear error if a count is unexpected.
  • Use f-strings for clarity and performance if running Python 3.6 or newer; fall back to .format() or older formatting on earlier interpreters (as suggested).
  • If running code under Python 2, remember print behaves differently there (or import the print function from __future__).

Additional notes

  • str.isupper() handles many Unicode uppercase letters, but behavior depends on the character category; test with representative input if international text is expected.
  • For very large strings, the generator+sum approach is memory-efficient and fast.

This complements the earlier replies and gives a concise, version-aware way to both compute the count and avoid the concatenation TypeError.

Recommended Answers

All 2 Replies

Member Avatar for Member #912166

upper is a integer - what your error is telling you is that you can't add strings and integers together.

You could do [1]

print ("The number of upper case letters are: " + str(upper))

Or better yet, use old style string formatting. %d is a specifier for integers, so I would do [2] -

print ("The number of upper case letters are: %d" % upper)

Or [3] -

print ("The number of upper case letters are: {}".format(upper))

Or if you're using Python3.x [4],

print ("The number of upper case letters are: ", upper)

[1] - http://docs.python.org/library/functions.html#str
[2] - http://docs.python.org/library/stdtypes.html#string-formatting-operations
[3] - http://docs.python.org/library/string.html#format-specification-mini-language
[4] - http://docs.python.org/release/3.2.1/library/functions.html#print

Note, if you aren't using Python 3.x, you don't need the brackets.

commented: Thorough +13

Thats really useful thanks.

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.