CodeNewbie Community ๐ŸŒฑ

Cover image for Python ๐Ÿ challenge_27โš”๏ธ
Mahmoud_Essam
Mahmoud_Essam

Posted on

Python ๐Ÿ challenge_27โš”๏ธ

Split Strings

  • Complete the solution so that it splits the string Into pairs of two characters.
  • If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').

Examples:

solution('abc')  should return => ['ab', 'c_']
solution('abcdef') should return => ['ab', 'cd', 'ef']
Enter fullscreen mode Exit fullscreen mode
Task URL: Link

My Solution:

def solution(s):
    step = 2
    if len(s) % 2 == 0:
        return  [s[index : index + step] for index in range(0, len(s), step)]
    else:
        odd_str = [s[index : index + step] for index in range(0, len(s), step)] 
        el = odd_str[-1] + '_'
        odd_str.pop()
        odd_str.append(el)
        return odd_str

Enter fullscreen mode Exit fullscreen mode

Code Snapshot:

Image description

Learn Python

Python top free courses from Coursera๐Ÿ๐Ÿ’ฏ๐Ÿš€

๐ŸŽฅ

Connect with Me ๐Ÿ˜Š

๐Ÿ”— Links

linkedin

twitter

Top comments (1)

Collapse
 
arletamiri8 profile image
wilson jackets

I forget to making a variable but this post help to find my problem in code. Thank you so much for this post keeps sharing more post like this. rick grimes crm jacket