
Make use of the split() method to split a string in Python,
By default when you do not pass any argument to the split method, the string is split by whitespace.
Example: Split Python String using whitespace"""
Sprint String in Python
Author: Code2care.org
Date: 03-Apr-2022
Version: 1.0
"""
my_string ="Hello there! Welcome to Python Programming!"
my_string_list = my_string.split()
for str in my_string_list:
print(str)
Output:
Hello
there!
Welcome
to
Python
Programming!
If you want to split a string using other characters like comma, semi-colon, pipe, new-line character or anything else, you can pass that delimited as an argument to the split method,
Example: Split Python String using commamy_string ="Python,Java,PHP,SharePoint"
my_string_list = my_string.split(",")
for str in my_string_list:
print(str)
Example: Split Python String using semi-colon
my_string ="Python;Java;PHP;SharePoint"
my_string_list = my_string.split(";")
for str in my_string_list:
print(str)
Example: Split Python String using new line characters (\r \n \r\n)
my_string ="Welcome\nto\ncode2care"
my_string_list = my_string.split("\n")
for str in my_string_list:
print(str)
-
Have Questions? Post them here!
More Posts related to Python,
- How to Install Python Modules in VS Code
- 3 Python program to add two numbers
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: How to get Current Directory
- Python - Convert float to String
- Tkinter - add x and y padding to label text
- Check if String Contains a Substring - Python
- Read JSON File in Python Program
- How to Convert String to DateTime in Python
- Sorting an array using Bubble Sort in Python Programming
- Python Hello World! Program with code example (snippet)
- How to delete a dir or folder using Python code
- Change label (text) color in tkinter
- Set width and height for the label in tkinter
- Check version of pip package installer for Python
- Reading JSON file in Python with Examples
- Indent Python code in Notepad++
- tkinter - Hello World! Program
- How to resolve Failed to create interpreter PyCharm Error
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- How to take user input from the console in a Python program
- Take input argument from command line in Python Programming
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Calculate discount amount python code
More Posts:
- How to Restart Apple Silicon based Mac (M1/M2 Chip) in Safe Mode - MacOS
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite - Mac-OS-X
- Disabling Spell Check in Android Studio - Android-Studio
- How to add password to pdf file for opening, editing, printing, copying - HowTos
- Hide Navigation Bar from Android Screen Activity - Android
- Unbound classpath container: JRE System Library [JavaSE-1.7] - Java
- Test Timeout using JUnit 4 with examples - Java
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome