Change language

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 

Python | Inverted split strings Python functions: Questions

Python | Inverted split strings split: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method