String-Length Tuples

Write a Python function that accepts a list of strings and returns a list of tuples where the first element is the string and the second is the length of the string.

Example 1:

Input: ['Hello', 'Python', 'World'] 
Output: [('Hello', 5), ('Python', 6), ('World', 5)]

Example 2:

Input: ['OpenAI', 'GPT', 'ChatGPT'] 
Output: [('OpenAI', 6), ('GPT', 3), ('ChatGPT', 7)]

Use the built-in len() function to find the length of each string.

def str_length_tuples(lst):
    return [(s, len(s)) for s in lst]

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!