Python | Inverted split strings



Method # 1: Using join () + reversed () + split ()

In this particular method, we first get the words of the elements, using the split function, reverse the order using the reverse function, and then perform the concatenation to link the elements together.

# Python3 demo code
# Reverse line splitting
# using join () + reversed () + split ()

 
# initialization string

test_string = " Gfg is best "

  
# print original line

print   ( "The original string:" + str (test_string))

  
# using join () + reversed () + split ()
# Reverse line splitting

res =   "," . join ( reversed (test_string.split ( "" )))

 
# print result

print ( "The string after reverse split : " + str (res)) < / td>

Output:

 The original string: Gfg is best The string after reverse split : best, is, Gfg 

Method # 2: Using join () + split () + slicing the list

This method is similar to the method described above, in which we perform splitting and concatenation, but the only difference in this method is that we use list slicing to perform the call.

# Python3 demo code
# Reverse line splitting
# using join () + split () + splitting the list

 
# initializing string

test_string = "Gfg is best"

 
# print original line

print ( " The original string: " + str (test_string))

 
# using join () + split () + slicing the list
# Reverse splitting the line

res =   `,` . join (test_string.split () [:: - 1 ])

  
# print result

print ( "The string after reverse split : " + str (res))

Output:

 The original string: Gfg is best The string after reverse split: best, is, Gfg