#๐Ÿ”’ Capitalization program not working

27 messages ยท Page 1 of 1 (latest)

drifting ridge
#

Anyone know what the problem may be? It says strings don't support object assignment, how else can i capitalize the letter I'm on? Also, if I can cut any corners on this code do let me know. I'm very new to python so please be kind

name = "the big brown fox jumped over the lazy dog"
for i in range(len(name)):
    if name[i:i+1] == name[i:i+1].lower() and name[i-1:i] == " ":
        name[i:i+1] = name[i:i+1].upper
    elif name[i:i+1] == name[i:i+1].lower() and i == 0:
        name[i:i+1] = name[i:i+1].upper```
bright driftBOT
#

@drifting ridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

compact arrow
#

Can you paste the entire error?

#

nevermind

#

you call a function using ()

drifting otter
#

You can't assign to a string's index

compact arrow
#

and that

drifting otter
#

!e

text = 'abcde'

text[3] = 'x'
bright driftBOT
# drifting otter !e ```py text = 'abcde' text[3] = 'x' ```

:x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 3, in <module>
003 |     text[3] = 'x'
004 |     ~~~~^^^
005 | TypeError: 'str' object does not support item assignment
compact arrow
#

strings are immutable

drifting otter
#

Even though it is indexable like a list, you can't assign to those indices

drifting ridge
#

What would I be able to do instead to achieve this goal then ๐Ÿ˜ฎโ€๐Ÿ’จ

compact arrow
#

What are you trying to do?

drifting ridge
#

I'm trying to capitalize every start of a new word

#

Including the start of a string

drifting otter
#

You can convert the string to a list so that you can do object assignment, then convert the list back to a str after

#

There's also a built-in method that does exactly that

#

but I assume this is to practice as an exercise

drifting ridge
drifting ridge
compact arrow
#

!e print(list("String"))

drifting ridge
#

And how would I convert it back

bright driftBOT
compact arrow
#

!d str.join

bright driftBOT
#

str.join(iterable)```
Return a string which is the concatenation of the strings in *iterable*. A [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) will be raised if there are any non-string values in *iterable*, including [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) objects. The separator between elements is the string providing this method.
bright driftBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.