Skip to content

Python input list of strings | Example code

  • by

Use the split() function on input string to get an input list of strings in Python. Where the split function splits a string into a list of words.

Example take an input list of strings in Python

A simple example code accepts the list of strings from a user in the format of a string separated by space.

input_string = input("Enter name separated by space: ")

# Split string into words
lst = input_string.split(" ")

print(lst)

Output:

Python input list of strings

Another example

# Taking input from the user for a list of strings
def get_input_list_of_strings():
    # Get the number of strings the user wants to input
    num_strings = int(input("Enter the number of strings: "))

    # Initialize an empty list to store the strings
    string_list = []

    # Loop to take input for each string and add it to the list
    for i in range(num_strings):
        string = input(f"Enter string {i+1}: ")
        string_list.append(string)

    return string_list

# Usage example
if __name__ == "__main__":
    input_list = get_input_list_of_strings()
    print("The list of strings you entered:", input_list)

When you run this code, it will prompt you to enter the number of strings you want to input. Then, it will ask you to enter each string one by one. After you’ve entered all the strings, it will print the list of strings you provided.

You can further modify and manipulate the input_list as per your requirements in the rest of your Python program.

Comment if you have any doubts or suggestions on this Python input list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading