
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)
Comments:
- Further comments disabled!
More Posts related to Python,
- Read JSON File in Python Program
- Convert Float to String in Python
- Python - Convert float to String
- Check if String Contains a Substring - Python
- Install and Run Jupyter Notebook on Mac (macOS)
- Sorting an array using Bubble Sort in Python Programming
- Comments in Python Programming
- 3 Python program to add two numbers
- Read a file line by line in Python Program
- How to uninstall pip Python packages
- Change label (text) color in tkinter
- 7 Python Arithmetic Operators with Examples [Tutorial]
- pip get list of all outdated Python packages
- Where does brew install python in macOS
- How to Convert String to DateTime in Python
- Python: Fix command not found pip or pip3 on zsh shell
- How to add borders to tkinter label text
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- TypeError: must be str, not int
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Check version of pip package installer for Python
- Change the background of Tkinter label or text
- Set width and height for the label in tkinter
- How to write JSON file in Python Program
- Python raise error with message example
More Posts:
- BeanDefinitionStoreException IOException parsing XML document from class path resource [spring.xml] - Java
- Python - Convert float to String - Python
- Enable spell check in Sublime Text (macOS) - MacOS
- How to upload file programmatically to SharePoint Document Library using Server Object Model C# .Net - SharePoint
- How to display Toast on Button Click : Android - Android