
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)
More Posts related to Python,
- How to add two float numbers in Python
- tkinter - Hello World! Program
- Convert Float to String in Python
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- How to List all Packages installed using pip [Python]
- Check if String Contains a Substring - Python
- Change label (text) color in tkinter
- Where does brew install python in macOS
- Validate email address in Python using regular expression (regex)
- How to uninstall pip Python packages
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Fix command not found pip or pip3 on zsh shell
- Check version of pip package installer for Python
- 3 Python program to add two numbers
- Calculate discount amount python code
- How to add borders to tkinter label text
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Comments in Python Programming
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to take user input from the console in a Python program
- Install and Run Jupyter Notebook on Mac (macOS)
- Tkinter - add x and y padding to label text
- 7 Python Arithmetic Operators with Examples [Tutorial]
- Python Sleep Function/Method Code Example
More Posts:
- Add Blank Lines Between Each Lines in Notepad++ - NotepadPlusPlus
- Fix error The tool was unable to install the Web Server (IIS) Role during SharePoint 2019 Setup - SharePoint
- How to make EditText text to uppercase or lowercase on macOS - MacOS
- PHP 301 Redirect Permanently - PHP
- Fix Microsoft Office 365 error code 135011 - Your organization has disabled this device - Microsoft