What is array.tounicode() in Python?

Overview

Python is a high-level programming language that provides functionalities for several operations. The array module comes with various methods to manipulate array data structures.

The function array.tounicode() converts an array to an unicode string.

Syntax

uni = array.tounicode()

Parameters

This function does not take in any arguments.

Return value

This function returns a unicode string. The array passed must be of type u. Otherwise, the function returns a ValueError.

Example

#import module
from array import array
#initialize array with unicode characters
array_one = array("u", ['a','b','c','d'])
#initialize array with unicode characters
array_two = array("u", ['e','f','g'])
print (array_one)
#convert array to unicode string
uni_one = array_one.tounicode()
print(uni_one)
print ("\n")
print (array_two)
#convert array to unicode string
uni_two = array_two.tounicode()
print(uni_two)

Explanation

  • Line 2: We import the array module.

  • Lines 5 and 8: We initialize arrays with unicode characters.

  • Lines 12 and 18: We convert arrays to unicode strings using array.tounicode().

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources