-2

I am looking a way to assign value to printed sentence to capitalize the first letters. The output should be: "Hello World!". This is my current code and the answer is "HELLO NEW WORLD" as a.title capitalizes every letter:

well='13H3e1llo0 312w20orl3d!432'
for a in well:
    if a.isnumeric()==False:
        print(a.title())

Any thoughts?

Tommy
  • 3
  • 2
  • 1
    Welcome to Stack Overflow. Did you try using separate steps in the code to figure out the string without digits, and *then* applying `title` to that? Do you understand how to *create a new string* from the letters, rather than `print`ing each one as it's found? – Karl Knechtel Oct 04 '22 at 06:56
  • The [python docs](https://docs.python.org/3/library/stdtypes.html) are very useful in this situation – bn_ln Oct 04 '22 at 06:58
  • 1
    Don't do things like `if a.isnumeric()==False:` (i.e. don’t compare boolean values to `True` or `False` using `==`). `a.isnumeric()` is already a bool. Just `if not a.isnumeric():` – buran Oct 04 '22 at 07:01
  • Does this answer your question? [How can I capitalize the first letter of each word in a string?](https://stackoverflow.com/questions/1549641/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string) – buran Oct 04 '22 at 07:04
  • Where does the `"NEW"` in `"HELLO NEW WORLD"` come from? – blhsing Oct 04 '22 at 07:17

1 Answers1

1

Here, a.title() works perfectly if we have a='hello world' but here you have used a for loop which means .title() is invoked for each alphabetical character and capitalizes it. We con do this way:

alphstr = ''
for char in well:
    if char.isnumeric() == False:
        alphstr += char
print(alphstr.title())

Hope you find it useful.

Shashi Kant
  • 117
  • 3