Python - Strings to Lists, Split Strings Into Lists, and Join Lists Into Strings

by matt392 in Circuits > Software

18 Views, 0 Favorites, 0 Comments

Python - Strings to Lists, Split Strings Into Lists, and Join Lists Into Strings

ListsStringSplitJoin.png
# Lists and Strings
# Join and Split

print ("Converting a string to a list: ")
DistributionOfLinux = "Debian"
ListOfLinux = list(DistributionOfLinux)
print ("The string is: ", DistributionOfLinux)
print ("The list is:", ListOfLinux, "\n")

print ("Splitting a string into a list.")
WhatIsLinux = "Linux is an interface between computer/server hardware, and the programs which run on it."
WhatIsLinuxList = WhatIsLinux.split()
print ("The string is: ", WhatIsLinux)
print ("The list is: ", WhatIsLinuxList, "\n")

print ("Split a string using a delimeter.")
DistributionsOfLinux = "Debian, Manjaro, Kali, antiX, LinuxMint, MxLinux, Slackware, OpenSUSE, Fedora"
delimeter = ","
SplitDistributionsList = DistributionsOfLinux.split(delimeter)
print ("The string to be split is: ", DistributionsOfLinux)
print ("The list split using the delimeter is: ", SplitDistributionsList, "\n")

print ("Concatenate the elements of a list into a string.")
DistributionsList = ["Debian", "Manjaro", "Fedora", "Kali", "Mint"]
delimeter = ", "
JoinedDistributionsList = delimeter.join(DistributionsList)
print ("The string to be joined is: ", DistributionsList)
print ("The string converted to a list is: ", JoinedDistributionsList)