-3

I started two months ago with coding so I am fairly new and I am working on a codewars challenge where I have to capitalize every word in a sentence and I am kinda lost right now.

I want the "t" in "aren't" to be small but my out put always comes out to "How Can Mirrors Be Real If Our Eyes Aren'T Real".

Additionally when I try to insert the a print method it says 'string' is not defined. Can someone explain me what I did wrong.

If possible don't tell me the solution but give me a hint on how to solve it.

This is my code

def to_jaden_case(string):
    
    string = ["how "+"can "+"mirrors "+"be "+"real "+"if "+"our "+"eyes "+"aren't 
    "+"real "]
    
    for letter in string:
             
        output = letter.title()
    
    return output 
martineau
  • 119,623
  • 25
  • 170
  • 301
Dustpie
  • 7
  • 2
  • use triple ` to format your code. – Leemosh Jan 15 '21 at 09:11
  • you cant use title for this if you want the `t` after the `'` to be lower case. This is explained and covered in the docs https://docs.python.org/3.8/library/stdtypes.html#str.title – Chris Doyle Jan 15 '21 at 09:19
  • I wouldn't give a parameter the name `string` because it collides with the name of the [`string`](https://docs.python.org/3/library/string.html) module from the Python Standard Library. Why is `string` a parameter anyway? You overwrite it in in the first line of the function. And why ist it called "string" if it is a `list`? – Matthias Jan 15 '21 at 09:53

4 Answers4

2

You should use capitalize() instead of title().

I am not sure why are you doing it this way, you may do something like:

capitalized = [word.capitalize() for word in ["how ","can ","mirrors ","be ","real ","if ","our ","eyes ","aren't ","real "]]

This is a list comprehension

Than you may convert the list back to string, using join.

wankata
  • 855
  • 4
  • 12
2

I'm not sure what format you're expecting from the input string, but the variable you've called string there is actually an array with a single string element.

The + operator simply concatenates multiple strings into 1, so:

string = ["how "+"can "+"mirrors "+"be "+"real "+"if "+"our "+"eyes "+"aren't 
    "+"real "]

is equivalent to:

string = ["how can mirrors be real if our eyes aren't real "]

And your loop is only going to execute once with the full string.

To operate on individual words you could try splitting the string by spaces:

string = "how can mirrors be real if our eyes aren't real"
words = string.split(" ")
output_words = []
for word in words:
    output_words.append(word.capitalize())
output_string = " ".join(output_words)

capitalize will only capitalize the first letter, whereas title operates on the first letter of each word (which includes the t). Because we split the string only on spaces "aren't" as treated as a single word so only the a is capitalized.

1

your function should receive a string. You can then split that string by space to get each word, capitlise each word and join all the words back up using a space.

def to_jaden_case(sentence):
    return " ".join(word.capitalize() for word in sentence.split())

original_sentence = "how can mirrors be real if our eyes aren't real"
new_sentence = to_jaden_case(original_sentence)
print(new_sentence)

OUTPUT

How Can Mirrors Be Real If Our Eyes Aren't Real
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
-1

The question is not too clear but what you need is probably just this:

' '.join(string)
mike_thecode
  • 171
  • 2
  • 6