Array to String in Python
What is an Array:
The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to a base value. Combining the arrays could speed up the process by condensing the overall amount of code. Multiple values are kept in a single variable using this method.
A group of things kept at adjacent memory locations is called an array. Keeping several of the same kind of items together is the idea. By adding an extra offset to a base value generally, the memory position of the initial element of the array denoted by the array name is now simpler to determine the position of each element.
The array module in Python is capable of dealing with arrays. They could be useful if we only need to work to certain primitive data values. The user is allowed to use lists as arrays. A list's elements cannot have their form restricted by the user. An array made with the aid of an array must have all elements of the same type.
Homogenous List Code:
list1=[1,2,3,4]
print(list1)
list2=['G','e','o','r','g', 'e']
print(list2)
list3=['Stacey', 'Jim', 'Ken']
print(list3)
OUTPUT:
[1, 2, 3, 4]
['G', 'e', 'o', 'r', 'g', 'e']
['Stacey', 'Jim', 'Ken']
Heterogenous List code:
L1=[1,'3','r']
print(L1)
L2=['2.2','Stella',1]
print(L2)
OUTPUT:
[1, '3', 'r']
['2.2', 'Stella', 1]
CODE:
import array as arr
ar = arr.array('i', [11, 22, 33, 44, 55])
print ("The array created is : ", end =" ")
for i in range (0, 5):
print (ar[i], end =" ")
print()
OUTPUT:
The array created is : 11 22 33 44 55
A Python module called array is capable of handling arrays. It is helpful when only a few data values need to be changed. To comprehend the idea of an array, consider the terms listed below:
- Element
- Index
Element: An element is any item saved in an array.
Index: An array's elements are found using a numerical index, representing their position in the array.
Strings concept in Python:
A Python string is a group of characters enclosed in single, double, or triple quotes. The computer does not understand the characters, which internally stores altered characters as a string of 0s and 1s. The ASCII character is used to encode each character. Thus, we can also refer to Python strings as Unicode characters. Python allows you to create strings by enclosing a character or string of characters in quotes. Python allows us to generate the string using single, double, or triple quotes.
The characters can be combined into a single or double quote string. Triple quotes are also available in Python to represent strings, but they are typically used for multiline strings or docstrings.
CODE:
#Creating a string using single quotes
str1 = 'This is a string1'
#Creating a string using double quotes
str2 = "This is a string2"
#Using triple quotes
str3 = ''' Typically, multiline
or document strings are
enclosed in triple quotes.'''
print(str1)
print(str2)
print(str3)
OUTPUT:
This is a string1
This is a string2
Typically, multiline
or document strings are
enclosed in triple quotes.
Array to String Conversion:
Using the join() function:
Using the join() function in Python is one of the easiest ways to accomplish this. It's crucial to keep in mind when employing this function that lists can only be converted into strings if all of their elements are strings.
CODE:
l=['My ','Name is ','Arthur.']
print(''.join(l))
OUTPUT:
My Name is Arthur.
Since each element in the list is a separate string, we can begin using the join function immediately. Keep in mind that a single space separates the new string's elements.
A list may now contain elements of a different data type than a string in some circumstances. The join function is not directly usable in this situation. The other data structure will first be converted into a string using the str() function, and then the join function is used.
Using a Function:
Adding each element to an empty string variable using a function.
Consider the following code illustrating the same.
Code:
def lstToStr(s):
str = ""
for ele in s:
str =str+ ele
return str
s = ['Welcome ', 'To ', 'TutorialAndExample']
print(lstToStr(s))
OUTPUT:
Welcome To TutorialAndExample
Using a map() function:
In two situations, a list can be turned into a string by using the map function:
- If the List contains only numbers.
- List is Heterogenous.
The str() function will change the specified data type into a string.
An Iterable sequence in which the str() function is called on every element. An iterator will be used to return the string values.
CODE:
L1=[1,2,3]
L2=['python ','programming']
ans1=''.join(map(str,L1))
ans2=''.join(map(str,L2))
print(ans1)
print(ans2)
OUTPUT:
123
python programming