Python Beginner lerning len

97 Views Asked by At

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?

5

There are 5 best solutions below

1
Dim971 On

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.
1
vegan_meat On

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.")
2
Thomas Kimber On

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

0
Sauron On
  • 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')

0
JohnyCapo On

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')