In: Computer Science
Create an array of ten strings. Write a program to join and split these strings. Feel free to use any whitespace character. Explain how the program works, when to use Python arrays, and when to use tuples, dictionaries, and sets. Explain how the program works and how it can be utilized to support an organization’s requirements.
n = int(input("Enter the number of strings you want to enter: "))
strings = [] # created a list to store numbers
for i in range (n):
strings.append(input("Enter the string: "))
print("Entered strings are: \n")
for i in range (n):
print(strings[i])
joined_strings = ""
print("Joined strings are: ")
joined_strings.join(strings)
joined_strings = ' '.join([str(elem) for elem in strings])
print(joined_strings)
split_string = joined_strings.split(" ")
print(split_string)
In the above program, string are captured into a list first. Then, using the join keyword, the list elements are appended to the string variable 'Joined_strings. Then after, the program uses split function on space delimeter to the 'joined_string' variable to split the string to multiple individual strings.
Tuples: Tuples are almost similar to Python lists. The distinction is that tuples are not modifiable. Less space is often used for tuples than lists. When we want a list of constants, we can use tuples. We may also use tuples as a key in the dictionary.
Sets: Where we do not want the collection to have redundant
elements.We have two different lists, and the common
elements between them are what we want. The best alternative for
this is the set. Unordered collections of basic elements are
set.
Dictionaries: When we require a logical association between a key
and value pair, there we need dictionary. If we want a username and
password to be kept. We should use a user name
dictionary as a key and a password as a value. Dictionary is also
mutable.
List: When we have a set of information that does not require random access, ,we can use lists. When we need a simple, iterable set that is updated regularly, we tend to select lists.