Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for First Word by mennadiego
import re
def first_word(text: str) -> str:
"""
You are given a string where you have to find its first word.
When solving a task pay attention to the following points:
- There can be dots and commas in a string.
- A string can start with a letter or, for example, one/multiple dot(s) or space(s).
- A word can contain an apostrophe and it's a part of a word.
- The whole text can be represented with one word and that's it.
:param text: input string
:return: str - first word of the string
"""
return re.split(r'[. ,]', text.strip('., '))[0]
print("Example:")
print(first_word("Hello world"))
# These "asserts" are used for self-checking
assert first_word("Hello world") == "Hello"
assert first_word(" a word ") == "a"
assert first_word("don't touch it") == "don't"
assert first_word("greetings, friends") == "greetings"
assert first_word("... and so on ...") == "and"
assert first_word("hi") == "hi"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 5, 2023
Comments: