Python Program to Split String by Multiple Separators

Split String by Multiple Separators Python Program

This python program is using split function in re python module and splits the string into multiple strings by multiple separators.

#!/usr/bin/python                                                               
                                                                                
import re                                                                       

# Split the string by multiple separators                                                                             
def split_string_by_multiple_separators(text):                                  
    strings = re.split('[:,]{1}', text)                                           
    return strings                                                              
                                                                                
if __name__ == "__main__":                                                      
    text = "hi, welcome to coding:pointer.com!"                                 
    strings = split_string_by_multiple_separators(text)                         
    print("Splited strings: ")                                                  
    for string in strings:                                                      
        print(string)   
        
Output:
$ python split-strings.py
Splited strings: 
hi
 welcome to coding
pointer.com!

This program splits the string by separators ':' and ','.

Split String by Multiple Separators including space

#!/usr/bin/python                                                               
                                                                                
import re                                                                       
                                                                                
def split_string_by_multiple_separators(text):                                  
    strings = re.split('[:,\s]{1}', text)                                          
    return strings                                                              
                                                                                
if __name__ == "__main__":                                                      
    text = "hi, welcome to coding:pointer.com!"                                 
    strings = split_string_by_multiple_separators(text)                         
    print("Splited strings: ")                                                  
    for string in strings:                                                      
        print(string)   
Output:
$ python split-strings.py
Splited strings: 
hi

welcome
to
coding
pointer.com!

This program splits the string by separators ':', ',' and ' '.

here {1} indicates exactly one occurrence either : or , are considered as separator.

Split String by more than one occurrence of multiple separators

Splits the string into multiple string by separator as substring matches the provided regular expression pattern.

    #!/usr/bin/python                                                               
                                                                                
import re                                                                       
                                                                                
def split_string_by_multiple_separators(text):                                  
    strings = re.split('[:,\s]+', text)                                         
    return strings                                                              
                                                                                
if __name__ == "__main__":                                                      
    text = "hi, welcome to coding:pointer.com!"                                 
    strings = split_string_by_multiple_separators(text)                         
    print("Splited strings: ")                                                  
    for string in strings:                                                      
        print(string)    
Output:
$ python split-strings.py
Splited strings: 
hi
welcome
to
coding
pointer.com!

here substring ', ' is also considered as separator and splited string into multiple strings.


Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^