w3resource

Python: Split a string into strings if there is a space in the string, otherwise split on commas, otherwise the list of lowercase letters with odd order

Python Programming Puzzles: Exercise-19 with Solution

Split String on Space or Comma

Write a Python program to split a given string (s) into strings if there is a space in s, otherwise split on commas if there is a comma, otherwise return the list of lowercase letters in odd order (order of a = 0, b = 1, etc.).

Input:
a b c d
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma, 
Output:
['a', 'b', 'c', 'd']

Input:
a,b,c,d
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma,
Output:
['a', 'b', 'c', 'd']

Input:
abcd
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma, 
Output:
['b', 'd']

Visual Presentation:

Python: Split a string into strings if there is a space in the string, otherwise split on commas, otherwise the list of lowercase letters with odd order.
Python: Split a string into strings if there is a space in the string, otherwise split on commas, otherwise the list of lowercase letters with odd order.
Python: Split a string into strings if there is a space in the string, otherwise split on commas, otherwise the list of lowercase letters with odd order.

Sample Solution:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a string 's' as input
def test(s):
    # Check if there is a space in the string 's'
    if " " in s:
        # Split the string into a list of strings using space as the delimiter
        return s.split(" ")
    # Check if there is a comma in the string 's'
    elif "," in s:
        # Split the string into a list of strings using comma as the delimiter
        return s.split(",")
    else:
        # Return a list of lowercase letters with odd ASCII values
        return [c for c in s if c.islower() and ord(c) % 2 == 0]

# Assign a specific string 'strs' to the variable
strs = "a b c d"

# Print the original string 'strs'
print("Original string:")
print(strs)

# Print a message indicating the operation to be performed
print("Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:")

# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign a different string 'strs' to the variable
strs = "a,b,c,d"

# Print the original string 'strs'
print("\nOriginal string:")
print(strs)

# Print a message indicating the operation to be performed
print("Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

# Assign another different string 'strs' to the variable
strs = "abcd"

# Print the original string 'strs'
print("\nOriginal string:")
print(strs)

# Print a message indicating the operation to be performed
print("Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

Sample Output:

Original string:
a b c d
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma, 
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']

Original string:
a,b,c,d
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma, 
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']

Original string:
abcd
Split the said string into strings if there is a space in the string, 
otherwise split on commas if there is a comma, 
otherwise return the list of lowercase letters with odd order:
['b', 'd']

Flowchart:

Flowchart: Python - Split a string into strings if there is a space in the string, otherwise split on commas, otherwise the list of lowercase letters with odd order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the indices of all occurrences of target in the uneven matrix.
Next: Determine the direction ('increasing' or 'decreasing') of monotonic sequence numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/puzzles/python-programming-puzzles-19.php