def capitalize_or_join_words(sentence):
"""
If the given sentence starts with *, capitalizes the first and last letters of each word in the sentence,
and returns the sentence without *.
Else, joins all the words in the given sentence, separating them with a comma, and returns the result.
For example:
- If we call capitalize_or_join_words("*i love python"), we'll get "I LovE PythoN" in return.
- If we call capitalize_or_join_words("i love python"), we'll get "i,love,python" in return.
- If we call capitalize_or_join_words("i love python "), we'll get "i,love,python" in return.
Hint(s):
- The startswith() function checks whether a string starts with a particualr character
- The capitalize() function capitalizes the first letter of a string
- The upper() function converts all lowercase characters in a string to uppercase
- The join() function creates a single string from a list of multiple strings
"""
if (sentence.startswith('*')):
list_sentence = ','.split(sentence)
list_car = []
list_sentence.pop(0)
for i in range(0,len(list_sentence),1):
list_car = ','.split(list_sentence[i])
for i in range(0,len(list_car),1):
if (i == 0 or i == len(list_car[i])):
list_car[i].upper()
list_car = ''.join(list_car)
sac = ' '.join(list_sentence)
return sac
else:
sentence = ','.join(sentence)
return sentence
Asked
Active
Viewed 137 times
-1

Andrej Kesely
- 168,389
- 15
- 48
- 91

Hassan Afif
- 19
-
1What is the question? Is there a problem with your program? – Andrej Kesely Sep 13 '22 at 09:10
-
it dose not work – Hassan Afif Sep 13 '22 at 09:12
-
In what way does it not work? – gvee Sep 13 '22 at 09:13
-
it does not return anything when it is called – Hassan Afif Sep 13 '22 at 09:21
-
How are you calling it? With what arguments? Is there an error? What is the expected output? – SitiSchu Sep 13 '22 at 09:22
-
1Try reading the code out loud to yourself. – Matt Clarke Sep 13 '22 at 09:26
-
for example: capitalize_ or_ join _ words (' * i love python') the output should be 'I LovE PythoN' but it returns nothing – Hassan Afif Sep 13 '22 at 09:26
-
Is this the entire contents of your .py file? – alexis Sep 13 '22 at 09:29
2 Answers
1
Possible Solution!
def capitalize_or_join_words(sentence):
"""
If the given sentence starts with *, capitalizes the first and last letters of each word in the sentence,
and returns the sentence without *.
Else, joins all the words in the given sentence, separating them with a comma, and returns the result.
For example:
- If we call capitalize_or_join_words("*i love python"), we'll get "I LovE PythoN" in return.
- If we call capitalize_or_join_words("i love python"), we'll get "i,love,python" in return.
- If we call capitalize_or_join_words("i love python "), we'll get "i,love,python" in return.
"""
if sentence.startswith('*'):
sentence = sentence[1:]
return ' '.join(
list(
map(
lambda x: x.upper() if len(x) == 1 else x[0].upper() + x[1:-1] + x[-1].upper(),
sentence.split()
)
)
)
else:
return ','.join(list(filter(lambda x: x != '', sentence.split(' '))))

Ashin Shakya
- 690
- 6
- 9
1
Here's a simplistic but effective solution:
def capitalize_or_join_words(sentence):
if sentence.startswith('*'):
tokens = []
for word in sentence[1:].split():
if len(word) < 3:
tokens.append(word.upper())
else:
tokens.append(word[0].upper() + word[1:-1] + word[-1].upper())
return ' '.join(tokens)
return ','.join(sentence.split())
print(capitalize_or_join_words('i love python'))
print(capitalize_or_join_words('*i love python'))
Output:
i,love,python
I LovE PythoN

DarkKnight
- 19,739
- 3
- 6
- 22