• We just launched and are currently in beta. Join us as we build and grow the community.

How to Split and Join a String in Python

imafatnoob

DAO Coordinator
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
83
Likes
39
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, we will program "How to Split and Join a String in the Console Terminal using Python." We’ll focus on splitting a string into segments and then rejoining it with a specified character or string. The objective is to split a given string safely and then join it with an additional character or separator to form a new string. A sample program will be provided to demonstrate the coding process.

This topic is straightforward to understand. Just follow the instructions I provide, and you'll be able to complete it yourself with ease. The program I'll show you demonstrates the correct way to split a string and then join it using a specific character. I'll also provide a simple and efficient method to achieve this effectively. So, let’s start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import

    re

  2. def

    split_string(

    string

    )

    :
  3. list_string =

    re

    .split

    (

    '\s

    +'

    ,

    string

    )
  4. return

    list_string

  5. def

    join_string(

    list_string)

    :
  6. new_string =

    '-'

    .join

    (

    list_string)
  7. return

    new_string

  8. if

    __name__ ==

    '__main__'

    :

  9. while

    True

    :
  10. print

    (

    "\n

    ================= Split and Join a String =================\n

    \n

    "

    )


  11. string

    =

    input

    (

    "Enter a String: "

    )


  12. list_string =

    split_string(

    string

    )
  13. print

    (

    list_string)

  14. new_string =

    join_string(

    list_string)
  15. print

    (

    new_string)

  16. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  17. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  18. ret=

    False
  19. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  20. ret=

    True
  21. print

    (

    "Exiting program...."

    )
  22. else

    :
  23. print

    (

    "Please enter yes/no:"

    )
  24. break

  25. if

    ret ==

    False

    :
  26. continue

This code allows users to input a string, which it then splits into a list of words using whitespace as the delimiter. After that, it joins the list of words into a new string with hyphens between each word. The program continues to prompt the user for new strings and performs the same operations until the user decides to exit by entering "no."

Output:

how-to-split-and-join-a-string-in-python-1.jpg

There you have it we successfully created How to Split and Join a String in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,009

327,017

Top