-3

I am doing some exercises, and I am a beginner in python. Doing some coding challenge and there was an exercise that tells me I should ask my name, and it should show me how many letters my name has.

My first solution was:

name = "What is your Name?"
print("Your Name has: " + len(name) + " Letters.")

It showed me an error.

Why was my first solution wrong? I mean, I learned a bit of Java it's a bit different, but shouldn't it normally print it out?
Would be nice if someone had different solutions or maybe a little answer for me.
Would be awesome, thanks.

I changed the len(name) into str(len(name)).

name = "What is your Name?"
print("Your Name has: " + str(len(name)) + " Letters.")

that worked.

but why my first solution didn't worked?

Aura
  • 1
  • 5
    Did you read an error that it gave you? – matszwecja Aug 18 '23 at 08:12
  • 2
    Unrelated: your code assumes that the user is named `What is your Name?`, rather than actually asking the user what their name is. – user2357112 Aug 18 '23 at 08:14
  • In First code you are adding `int` to `string` therefore in your second code when you `typecast` it to `string` it worked. you can use f-strings like `print(f'Your name has {len(name)} letters')` – Sauron Aug 18 '23 at 08:19
  • Does this answer your question? [How to resolve TypeError: can only concatenate str (not "int") to str](https://stackoverflow.com/questions/51252580/how-to-resolve-typeerror-can-only-concatenate-str-not-int-to-str) – Abdul Aziz Barkat Aug 18 '23 at 08:36

5 Answers5

2

In java, every primitive type has an innate string-like wrapper that kicks in when you build a string using different components and the + operator.

That convention isn't adopted in python.

There are different ways to build a string in python, the simplest, using the + operator, requires all components be cast to strings, as you have discovered.

I'd also recommend looking at the string format function, as well as f-strings which is an alternative.

For a more rounded, general information source, try the python Input and Output docs

Thomas Kimber
  • 10,601
  • 3
  • 25
  • 42
  • Well said. Totally agree – JohnyCapo Aug 18 '23 at 08:31
  • Okay understand. In java will be too everything declared thats a bit different in python, didnt thought that much about it. So if i want to use a int in a string i need to format it or use f-string. Will be looking into it thanks for the advice. – Aura Aug 18 '23 at 09:10
0

Because you cannot append a number to a string: "TypeError: can only concatenate str (not "int") to str"

Better to use f-strings:

>>> print(f"Your Name has: {len(name)} Letters.")
Your Name has: 18 Letters.
Dim971
  • 13
  • 1
  • 4
0

Java and Python are different languages. Java has a String concatenate that will "promote" an int to a String.

name = "What is your Name?"
    print("Your Name has: " + len(name) + " Letters.")

In your first code your len(name) is an int type and your trying to concatenate into a string which throws error

TypeError: can only concatenate str (not "int") to str

Python works a bit differently, the value you are concatenating needs to be same type, both int or str. To concatenate a string and a number, such as an integer int or a floating point number float , you first need to convert the number to a string with str() . Then, you can use the + or += operator to concatenate.

but you can also use f-string. f-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.like this

name = str(input("what is your name ?"))
print(f"Your Name has: {len(name)} Letters.")
vegan_meat
  • 878
  • 4
  • 10
  • Thanks a lot for the Explanation. I know they are 2 different languages, sure. I was just a bit confused about the operator, thought it would be the same like in Java, but forgot in java you need to declare everything and in python is seems like everything a string. I got a long road to go, but i like :) python it makes fun to code. So i need to convert everything in python. Thanks for the explanation, i will read into f-string. – Aura Aug 18 '23 at 09:06
0
  • In your First Code you are adding int to str, this is TypeError in Python, Therefore when you typecast it to str(len(name)) it works.

  • Use f-string. so you don't have to concatenate the strings like

    print(f'Your name has: {len(name)} letters')

Sauron
  • 551
  • 2
  • 11
0

For example take this input:

name = 'What is your name?'  # This crates stringVar 
print(len(name))             # This works because Python can easily print integer
print(len(name) + 25)        # Python can also print math functions
try:
    print('My name is ' + len(name) + 'letter long')  # But this here is called concatenation which must be created with strings -> that's why the error
except TypeError as er:
    print(er)
print(f'My name is {len(name)} letters long')         # This works because this is f-string

The output:

enter image description here

What is f-string?

If you want the name to be really interactive use it like this:

name = input('What is your name:\t')

JohnyCapo
  • 170
  • 10