{"id":24573,"date":"2021-10-22T09:08:52","date_gmt":"2021-10-22T03:38:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=24573"},"modified":"2021-11-05T19:39:16","modified_gmt":"2021-11-05T14:09:16","slug":"python-ord-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-ord-function-with-examples\/","title":{"rendered":"Python ord() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for round() Function<\/a> The ord() function returns a number that represents the Unicode code of a given character.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n character:<\/strong> It is a Unicode character.<\/p>\n Return Value:<\/strong><\/p>\n The ord() function returns the Unicode character as an integer.<\/p>\n Note:<\/strong> When the length of the string is not equal to one, a TypeError is thrown.<\/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 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 Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for round() Function ord() Function in Python: The ord() function returns a number that represents the Unicode code of a given character. Syntax: ord(character) Parameters character: It is a Unicode character. Return Value: The ord() function returns the Unicode character as an integer. Note: When the …<\/p>\n
\nord() Function in Python:<\/strong><\/p>\nord(character)<\/pre>\n
Given character = '4'\r\nGiven character = '$'\r\nGiven character = 'd'<\/pre>\n
The integer representing the given Unicode character 4 = 52\r\nThe integer representing the given Unicode character $ = 36\r\nThe integer representing the given Unicode character d = 100<\/pre>\n
Given character = '0'\r\nGiven character = '@'\r\nGiven character = 'y'<\/pre>\n
The integer representing the given Unicode character 0 = 48\r\nThe integer representing the given Unicode character @ = 64\r\nThe integer representing the given Unicode character y = 121<\/pre>\n
Program for ord() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the character as static input and store it in a variable.\r\ngvn_charctr = '4'\r\n# Apply ord() function to the given character which returns a number that\r\n# represents the Unicode code of a given character.\r\n# Store it in another variable.\r\ncharctrs_unicode = ord(gvn_charctr)\r\n# Print the integer representing the above given Unicode character.\r\nprint(\"The integer representing the given Unicode character\",\r\n gvn_charctr, \"= \", charctrs_unicode)\r\n<\/pre>\n
The integer representing the given Unicode character 4 = 52<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the character as user input using the input() function and store it in a variable.\r\ngvn_charctr = input(\"Enter some random character = \")\r\n# Apply ord() function to the given character which returns a number that\r\n# represents the Unicode code of a given character.\r\n# Store it in another variable.\r\ncharctrs_unicode = ord(gvn_charctr)\r\n# Print the integer representing the above given Unicode character.\r\nprint(\"The integer representing the given Unicode character\",\r\n gvn_charctr, \"= \", charctrs_unicode)\r\n<\/pre>\n
Enter some random character = B\r\nThe integer representing the given Unicode character B = 66<\/pre>\n
\n