How to Read File and Convert to Ascii Text Using Python

Introduction

In python, we have discussed many concepts and conversions. Only sometimes, nosotros come to a situation where we need to convert ASCII into string in python. In this tutorial, we will be discussing how to convert ASCII into strings in python. As the conversion of elements has been a handy utility as it offers it in a much simpler way than other languages.

What are ASCII Values?

ASCII stands for American Standard Code for Data Interchange. It is a graphic symbol encoding that uses numeric codes to correspond characters from 0 to 127. These include upper and lowercase English letters, numbers, and punctuation symbols. For example, the ASCII code for character A is 65, and Z is 90.

Means to convert ASCII into string in python

Here, we volition exist discussing all the different ways through which nosotros can convert ASCII into string in python:

1. Using chr() role to convert ASCII into cord

In this example, nosotros will be using the chr() role with ASCII value equally a parameter to function. We will be taking an input list with some integer values. Later on that, we will be using the chr() function, which volition convert the ASCII values into integers. At final, we will print the resultant string. Let us look at the example for agreement the concept in particular.

# Input list lst = [80, 121, 116, 104, 111, 110, 		eighty, 111, 111, 108]  # Printing initial list print ("Input list", lst)  # Using chr() Method res = "" for i in lst: 	res = res + chr(i)  print ("String : ", str(res))          

Output:

chr() function to convert ASCII into string

Explanation:

  • Firstly, we will be taking an input list with some ASCII values.
  • So, we will be printing the input list.
  • And then, we will exist taking an empty string res.
  • After that, we will use for loop till the length of cord, and inside information technology, we volition be using the chr() function to convert ASCII value into graphic symbol value.
  • At last, we will print the Final string.
  • Hence, y'all tin can see the ASCII values get converted into a string.

2. Using join and list comprehension to convert ASCII into string

In this example, nosotros will be using join and list comprehension with the help of the chr() part to catechumen each ASCII value to its respective character. bring together(iterable) method withstr as"" to concatenate each character in iterable into a single string. At last, we will print the final string. Allow us look at the instance for agreement the concept in particular.

# Input list lst = [80, 121, 116, 104, 111, 110, 		eighty, 111, 111, 108]  # Printing input list impress ("Initial list : ", lst)  # Using listing comprehension and join res = ''.join(chr(i) for i in lst)  print ("Final cord : ", str(res))          

Output:

join and list comprehension

Explanation:

  • Firstly, we will be taking an input list with some ASCII values.
  • Then, nosotros volition be printing the input list.
  • After that, we volition exist taking a variable 'res' to apply the join() method.
  • Within the join() method, We take used listing comprehension, which will convert the ASCII into the string.
  • At concluding, nosotros will impress the Final string.
  • Hence, you can see the ASCII values get converted into strings.

3. Using map() function to convert ASCII into string

In this case, we will be using the map() function and join() method to convert each ASCII value to its corresponding character. join(iterable) method with str every bit" " to concatenate each graphic symbol in iterable into a unmarried cord. At final, nosotros volition print the final cord. Let us await at the example for understanding the concept in detail.

# Input list lst = [80, 121, 116, 104, 111, 110, 		80, 111, 111, 108]  # Press input list impress ("Initial list : ", lst)  # Using map and join res = ''.join(map(chr, lst))  print ("Final string : ", str(res))          

Output:

map() function to convert ASCII into string

Explanation:

  • Firstly, nosotros volition be taking an input listing with some ASCII values.
  • And then, we will be press the input list.
  • After that, we will be taking a variable 'res' to apply the bring together() method.
  • Inside the bring together() method, nosotros have used the map() function through which ASCII values get converted into strings.
  • At terminal, we will print the Last cord.
  • Hence, you can run across the ASCII values go converted into strings.

Decision

In this tutorial, we have learned about the concept of conversion of ASCII into string in python. Nosotros have also discussed what ASCII values are? Then, we take discussed all the different ways with the help of an case through which we can catechumen ASCII values into strings in python. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.

All the same, if you have any doubts or questions, exercise permit me know in the annotate section beneath. I will try to aid you as presently as possible.

FAQs

ane. How to bank check if a string in Python is in ASCII?

We can check if the cord in python is ASCII or not with the assist of the post-obit lawmaking:

def is_ascii(south):     render all(ord(c) < 128 for c in s)          

In this, s is the string through which you can check if the python string is ASCII or not. You lot tin can write whatever of the strings and run the program. It will requite you the output in True or Fake.

two. Are python strings ASCII?

Python strings have no holding corresponding to 'ASCII' or any other encoding. The input of your string (whether you read it from a file, input from a keyboard, etc.) may take encoded a Unicode string in ASCII to produce your cord. In case you lot want to remove all the Unicode charactersfrom the string in python. You tin can read our in-depth article from here.

iii. What does chr() do in Python?

The chr() function in python inputs as an integer, a Unicode code signal, and converts that into a string representing a character. This function is used to return a cord with length 1, representing the character whose Unicode code indicate value is passed in the chr() part.

simssposing1991.blogspot.com

Source: https://www.pythonpool.com/python-ascii-to-string/

0 Response to "How to Read File and Convert to Ascii Text Using Python"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel