Automate the boring stuff with Python: Comma Code

前端 未结 29 1304
深忆病人
深忆病人 2021-02-03 16:17

Currently working my way through this beginners book and have completed one of the practice projects \'Comma Code\' which asks the user to construct a program which:

29条回答
  •  無奈伤痛
    2021-02-03 16:36

    I tried this, hope this is what you are looking for :-

    spam= ['apples', 'bananas', 'tofu', 'cats']
    
    def list_thing(list):
    
    #creating a string then splitting it as list with two items, second being last word
        new_string=', '.join(list).rsplit(',', 1)    
    
    #Using the same method used above to recreate string by replacing the separator.
    
        new_string=' and'.join(new_string)
        return new_string
    
    print(list_thing(spam))
    

提交回复
热议问题