#Could someone help me with this assignment?
4 messages · Page 1 of 1 (latest)
Thanks for your question :clap:, if someone gives you an answer it would be great if you thanked them with a :white_check_mark: in response. This response will earn you both points for special roles on this server.
To keep the capitalization and punctuation in place when reversing a string in Python, you can use the following steps:
- Split the string into a list of characters.
- Reverse the list of characters.
- Join the list of characters back into a string.
Here is a Python function that implements these steps:
def word_reversal(msg):
"""Reverses a string, keeping the capitalization and punctuation in place.
Args:
msg: A string to reverse.
Returns:
A reversed string, with the capitalization and punctuation preserved.
"""
chars = list(msg)
chars.reverse()
reversed_msg = ''.join(chars)
return reversed_msg
This function can be used to reverse the strings in the test cases you provided as follows:
>>> word_reversal('Woah, this problem seems cool!')
'Haow, siht melborp smees looc!'
>>> word_reversal('Quoth the Raven "Nevermore."')
'Htouq eht Nevar "Eromreven."
>>> word_reversal('CaPs Like This Give Me CaNcEr')
'SpAc Ekll SiHt Evlg Em ReCnAc
>>> word_reversal('Don\'t use, too! manY; "punctuations"? OR my ^CoDe^ might@ (not) work!?!')
'T\'nod esu, oot! ynaM; "snoitautcnup"? RO ym ^EdOc^ thgim@ (ton) krow!?!'
As you can see, the capitalization and punctuation of the reversed strings is preserved.
the call for help is some days ago - but the upper solution ... is there something of the code missing - nevertheless, the task is interesting for little exercises
@bleak fern do you have meanwhile a solution by your teacher ?
my idea is here: