Python 2.3.1.18 LAB: Your Own Split

 Python LAB: 2.3.1.18 Your Own Split Function

Your Own Split Function
2.3.1.18 Your Own Split

Objectives

  • improving the student's skills in operating with strings;
  • using built-in Python string methods.

Scenario

You already know how split() works. Now we want you to prove it.
Your task is to write your own function, which behaves almost exactly like the original split() method, i.e.:
  • it should accept exactly one argument - a string;
  • it should return a list of words created from the string, divided in the places where the string contains whitespaces;
  • if the string is empty, the function should return an empty list;

  • its name should be mysplit()
     
    Your Own Split Function
    Lab Problem

Use the template in the editor. Test your code carefully.

Expected output

['To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
['To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[]
['abc']
[]

Solution Code:

Python is all about simplicity, and in the example we haven't been prohibited from using split() method. So, we are going to use the method inside our function.

def mysplit(strng):
    return strng.split()

print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit("   "))
print(mysplit(" abc "))
print(mysplit(""))

=================================================================================

Follow the Python page on the Blog to be the first to know ❤

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

7 Reasons Why You Need to Learn English

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst

15 Python Builtin-Functions You Need to Master as a Beginner

Prefixes vs Suffixes ? What are they?

Python 4.3.1.7 LAB: How many days: writing and using your own functions

Python 4.3.1.6 LAB: A leap year: writing your own functions