5 Questions for TYL on Strings (reverse words in string)
Question 1
Stuff you need to know :
split() method
for-each iteration style
append operator / in-built function
If you did not split the string and looped through it, it would go character by character.
w = "I Love CMRIT"
for ch in w:
print(ch)
Output:
I
L
o
v
e
C
M
R
I
T
Code :
for word in w.split():
print(word)
So, use split(), by default, split separates out spaces, so you don't have to specify any delimiter.
I
Love
CMRIT
Next, it is a matter of reorganizing it back to front and store it in another variable. Remember to add the next word before the current contents to get the reversed effect.
w = "I Love CMRIT"
s=""
for word in w.split():
s=word+s
print(s)
Output:
CMRITLoveI
Also, notice in the question, the space is intact... so we'll manually add that in also.
Final Program:
w = "I Love CMRIT"
s=""
for word in w.split():
s=word+" "+s
print(s)
Output
CMRIT Love I
Alternately.... you can reverse a list that gets generated with w.split()
Try this out in the interpreter mode:
>>> w="I Love Cmrit"
>>> w.split()
['I', 'Love', 'Cmrit']
>>>
['I', 'Love', 'Cmrit'] is a list
Now to reverse it... all that's required is... use a double :: followed by a -1, this will take the last item, then loop back to the beginning.
>>> s=s[::-1]
>>> s
['Cmrit', 'Love', 'I']
You can just print it out as it it... remember to end the print statement with a " " instead of the default new line character.
Final Alternate Program:
w = "I Love CMRIT"
s = w.split()
rev = s[::-1]
for word in rev:
print(word, end=" ")
Output
CMRIT Love I
1. Write a program to reverse only words in a
string
I Love Cmrit
Cmrit Love I
Stuff you need to know :
split() method
for-each iteration style
append operator / in-built function
If you did not split the string and looped through it, it would go character by character.
w = "I Love CMRIT"
for ch in w:
print(ch)
Output:
I
L
o
v
e
C
M
R
I
T
Code :
for word in w.split():
print(word)
So, use split(), by default, split separates out spaces, so you don't have to specify any delimiter.
I
Love
CMRIT
Next, it is a matter of reorganizing it back to front and store it in another variable. Remember to add the next word before the current contents to get the reversed effect.
w = "I Love CMRIT"
s=""
for word in w.split():
s=word+s
print(s)
Output:
CMRITLoveI
Also, notice in the question, the space is intact... so we'll manually add that in also.
Final Program:
w = "I Love CMRIT"
s=""
for word in w.split():
s=word+" "+s
print(s)
Output
CMRIT Love I
Alternately.... you can reverse a list that gets generated with w.split()
Try this out in the interpreter mode:
>>> w="I Love Cmrit"
>>> w.split()
['I', 'Love', 'Cmrit']
>>>
['I', 'Love', 'Cmrit'] is a list
Now to reverse it... all that's required is... use a double :: followed by a -1, this will take the last item, then loop back to the beginning.
>>> s=s[::-1]
>>> s
['Cmrit', 'Love', 'I']
You can just print it out as it it... remember to end the print statement with a " " instead of the default new line character.
Final Alternate Program:
w = "I Love CMRIT"
s = w.split()
rev = s[::-1]
for word in rev:
print(word, end=" ")
Output
CMRIT Love I
Comments
Post a Comment