Open In App

Zip function in Python to change to a new character set

Last Updated : 27 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd….xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. 

Examples:

New character set : qwertyuiopasdfghjklzxcvbnm
Input : "utta"
Output : geek

Input : "egrt"
Output : code

We have existing solution for this problem please refer Change string to a new character set link. We will solve this problem in python using Zip() method and Dictionary data structures. Approach is simple,

  1. Create a dictionary data structure where we will map original character set in english language with new given character set, zip(newCharSet,origCharSet) does it for us. It will map each character of original character set onto each given character of new character set sequentially and return list of tuples of pairs, now we convert it into dictionary using dict().
  2. Now iterate through original string and convert it into new string.

Implementation:

Python3




# Function to change string to a new character
 
def newString(charSet,input):
 
    # map original character set of english
    # onto new character set given
    origCharSet = 'abcdefghijklmnopqrstuvwxyz'
    mapChars = dict(zip(charSet,origCharSet))
 
    # iterate through original string and get
    #  characters of original character set
    changeChars = [mapChars[chr] for chr in input]
 
    # join characters without space to get new string
    print (''.join(changeChars))
 
# Driver program
if __name__ == "__main__":
    charSet = 'qwertyuiopasdfghjklzxcvbnm'
    input = 'utta'
    newString(charSet,input)


Output

geek

Similar Reads

Change string to a new character set
Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd....xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. Examples: New character set : qwertyuiopasdfghjklzxcvbnm Input : "utta" Output : geek Input : "egrt" Output :
7 min read
Count of strings possible by replacing two consecutive same character with new character
Given string str. The task is to count the number of all different strings possible if two consecutive same characters of the string can be replaced by one different character. Examples Input: str = "abclll" Output: 3 Explanation: There can be 3 different string including the original string as shown in the below figure:- Input: str = "abcllldefkkk
8 min read
Python | Zip different sized list
In Python, zipping is a utility where we pair one list with the other. Usually, this task is successful only in the cases when the sizes of both the lists to be zipped are of the same size. But sometimes we require that different sized lists also to be zipped. Let's discuss certain ways in which this problem can be solved if it occurs. Method #1 :
4 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Python | Zip Uneven Tuple
In Python, zipping is a utility where we pair one record with the other. Usually, this task is successful only in cases when the sizes of both the records to be zipped are of the same size. But sometimes we require that different-sized records also be zipped. Let’s discuss certain ways in which this problem can be solved if it occurs. Method #1 : U
4 min read
Application to get address details from zip code Using Python
Prerequisite: Tkinter In this article, we are going to write scripts to get address details from a given Zip code or Pincode using tkinter and geopy module in python, Tkinter is the most commonly used GUI module in python to illustrate graphical objects and geopy is a python module to locate the coordinates of addresses, cities, countries, landmark
2 min read
Get Zip Code with given location using GeoPy in Python
In this article, we are going to write a python script to get the Zip code by using location using the Geopy module.geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the world. To install the Geopy module, run the following command in your terminal. pip install geopy Approach:
2 min read
Python program for Zip, Zap and Zoom game
Zip zap zoom is a fun game that requires its players to concentrate and participate actively. When this game is played with people, this is how it is played: Players stand in a circle, 6 feet away from each otherOne player starts the game by clap-pointing and saying zap to the person on its leftThis person now does the same to the person to its rig
3 min read
Create Password Protected Zip of a file using Python
ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a single file containing one or more compressed files, offering an ideal way to make large files smaller and keep re
2 min read
How to Brute Force ZIP File Passwords in Python?
In this article, we will see a Python program that will crack the zip file's password using the brute force method. The ZIP file format is a common archive and compression standard. It is used to compress files. Sometimes, compressed files are confidential and the owner doesn't want to give its access to every individual. Hence, the zip file is pro
3 min read
Article Tags :
Practice Tags :