Tutorials  /  Python

Python Convert String to List

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

We can convert a string to list in Python using split() function. Python String split() function syntax is:

Code
str.split(sep=None, maxsplit=-1)

Example: Converting String to List of Words

Let’s look at a simple example where we want to convert a string to list of words i.e. split it with the separator as white spaces.

Python
s = 'Welcome To JournalDev'
print(f'List of Words ={s.split()}')

Output:

Code
List of Words =['Welcome', 'To', 'JournalDev']

If you are not familiar with f-prefixed string formatting, please read f-strings in Python

If we want to split a string to list based on whitespaces, then we don’t need to provide any separator to the split() function. Also, any leading and trailing whitespaces are trimmed before the string is split into a list of words. So the output will remain same for string s = ' Welcome To JournalDev ' too. Let’s look at another example where we have CSV data into a string and we will convert it to the list of items.

Python
s = 'Apple,Mango,Banana'
print(f'List of Items in CSV ={s.split(",")}')

Output:

Code
List of Items in CSV =['Apple', 'Mango', 'Banana']
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Python String to List of Characters

Python String is a sequence of characters. We can convert it to the list of characters using list() built-in function. When converting a string to list of characters, whitespaces are also treated as characters. Also, if there are leading and trailing whitespaces, they are part of the list elements too.

Python
s = 'abc$ # 321 '
print(f'List of Characters ={list(s)}')

Output:

Code
List of Characters =['a', 'b', 'c', '$', ' ', '#', ' ', '3', '2', '1', ' ']

If you don’t want the leading and trailing whitespaces to be part of the list, you can use strip() function before converting to the list.

Python
s = ' abc '
print(f'List of Characters ={list(s.strip())}')

Output:

Code
List of Characters =['a', 'b', 'c']

Conclusion

That’s all for converting a string to list in Python programming. If you are looking for customized solutions for your business or need further consultation, get in touch with our sales team now – we are happy to assist you!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren