[Python] How to write a function that returns True if the parameter 'string' starts and ends...

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    Here is my assignment:


    Write a function x_at_either_end(string) that takes a string string as a parameter, which may be empty, and returns a boolean. It returns True if and only if the string string starts or ends with the lower-case character x.

    We recommend using the startswith and endswith methods of str.
    ​

    def x_at_either_end(string):
    '''Returns true if string parameter starts and ends with
    lowercase letter'''
    if (string.startswith(string, x )):
    return True
    if (string.endswith(string, x )):
    return True
    return False

    print(x_at_either_end('Ping pong'))
    print(x_at_either_end('pax'))
    print(x_at_either_end('xposure'))

    Continue reading...

Compartilhe esta Página