{"id":17601,"date":"2021-08-26T10:42:38","date_gmt":"2021-08-26T05:12:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=17601"},"modified":"2021-11-22T18:37:12","modified_gmt":"2021-11-22T13:07:12","slug":"python-program-to-convert-each-character-in-a-string-to-an-ascii-value","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-each-character-in-a-string-to-an-ascii-value\/","title":{"rendered":"Python Program to Convert each Character in a String to an ASCII Value"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Divide a String in ‘N’ Equal Parts.<\/a> Given a string and the task is to convert each character in a given String to an ASCII Value.<\/p>\n ASCII Characters:<\/strong><\/p>\n The standard range of ASCII, which stands for American Standard Code for Information Interchange, is \u201cZero\u201d to \u201cOne Hundred and Twenty Seven\u201d.<\/p>\n ASCII codes are used to represent text in computers and other electronic devices. The character-encoding schemes used in most modern telecommunications equipment are based on ASCII.<\/p>\n As a result, everything else falls into the category of \u201cnon-ASCII\u201d character.<\/p>\n ord() function:<\/strong> To determine the specific ASCII value of that character.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert each character in a given String to an ASCII Value.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Divide a String in ‘N’ Equal Parts. When each character in a string is converted to an ASCII value, a list of corresponding ASCII values for each character is returned. Converting each character in “dce” to an ASCII value, for example, yields [100, 99, 101]. …<\/p>\n
\nWhen each character in a string is converted to an ASCII value, a list of corresponding ASCII values for each character is returned. Converting each character in “dce” to an ASCII value, for example, yields [100, 99, 101].<\/p>\nGiven String = \" btechgeeks\"<\/pre>\n
The given string after convering each character to an ASCII value : [98, 116, 101, 99, 104, 103, 101, 101, 107, 115]<\/pre>\n
Given String = \"hello this is btechgeeks\"<\/pre>\n
The given string after convering each character to an ASCII value : [104, 101, 108, 108, 111, 32, 116, 104, 105, 115, 32, 105, 115, 32, 98, 116, 101, 99, 104, 103, 101, 101, 107, 115]<\/pre>\n
Program to Convert each Character in a String to an ASCII Value.<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello this is btechgeeks\"\r\n# Take an empty list say \"ASCII_vals\" and store it in another variable.\r\nASCII_vals = []\r\n# Loop the above-given string using the for loop.\r\nfor itr in gvn_str:\r\n # Calculate the ASCII value of the iterator using the ord() function\r\n # and store it in another variable.\r\n a = ord(itr)\r\n# Append the above obtained ASCII value of the iterator to the above-initialized\r\n# list \"ASCII_vals\" using the append() method.\r\n ASCII_vals.append(a)\r\n # Print the given string after converting each character to an ASCII value.\r\nprint(\"The given string after convering each character to an ASCII value :\", ASCII_vals)\r\n<\/pre>\n
The given string after convering each character to an ASCII value : [104, 101, 108, 108, 111, 32, 116, 104, 105, 115, 32, 105, 115, 32, 98, 116, 101, 99, 104, 103, 101, 101, 107, 115]<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Take an empty list say \"ASCII_vals\" and store it in another variable.\r\nASCII_vals = []\r\n# Loop the above-given string using the for loop.\r\nfor itr in gvn_str:\r\n # Calculate the ASCII value of the iterator using the ord() function\r\n # and store it in another variable.\r\n a = ord(itr)\r\n# Append the above obtained ASCII value of the iterator to the above-initialized\r\n# list \"ASCII_vals\" using the append() method.\r\n ASCII_vals.append(a)\r\n # Print the given string after converting each character to an ASCII value.\r\nprint(\"The given string after convering each character to an ASCII value :\", ASCII_vals)\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nThe given string after convering each character to an ASCII value : [98, 116, 101, 99, 104, 103, 101, 101, 107, 115]<\/pre>\n
\n