{"id":162,"date":"2020-10-04T00:28:29","date_gmt":"2020-10-04T00:28:29","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=162"},"modified":"2025-03-26T06:47:21","modified_gmt":"2025-03-26T06:47:21","slug":"python-type-conversion","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-type-conversion\/","title":{"rendered":"Python Type Conversion"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about type conversion in Python and some useful type conversion functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-type-conversion-in-python'>Introduction to type conversion in Python <a href=\"#introduction-to-type-conversion-in-python\" class=\"anchor\" id=\"introduction-to-type-conversion-in-python\" title=\"Anchor for Introduction to type conversion in Python\">#<\/a><\/h2>\n\n\n\n<p>To get input from users, you use the <code>input()<\/code> function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">value = input(<span class=\"hljs-string\">'Enter a value:'<\/span>)\nprint(value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=dmFsdWUgPSBpbnB1dCgnRW50ZXIgYSB2YWx1ZTonKQpwcmludCh2YWx1ZSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>When you execute this code, it&#8217;ll prompt you for input on the Terminal:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter a value:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you enter a value, for example, a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-numbers\/\">number<\/a>, the program will display that value back:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter a value:<span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-number\">100<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">Note that the Playground will display an input dialog instead of a prompt.<\/p>\n\n\n\n<p>However, the <code>input()<\/code> function returns a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">string<\/a>, not an <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\">integer<\/a>. <\/p>\n\n\n\n<p>The following example prompts you to enter two input values: net price and tax rate. After that, it calculates the tax and displays the result on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = input(<span class=\"hljs-string\">'Enter the price ($):'<\/span>)\ntax = input(<span class=\"hljs-string\">'Enter the tax rate (%):'<\/span>)\n\ntax_amount = price * tax \/ <span class=\"hljs-number\">100<\/span>\n\nprint(<span class=\"hljs-string\">f'The tax amount price is $<span class=\"hljs-subst\">{tax_amount}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cHJpY2UgPSBpbnB1dCgnRW50ZXIgdGhlIHByaWNlICgkKTonKQp0YXggPSBpbnB1dCgnRW50ZXIgdGhlIHRheCByYXRlICglKTonKQoKdGF4X2Ftb3VudCA9IHByaWNlICogdGF4IC8gMTAwCgpwcmludChmJ1RoZSB0YXggYW1vdW50IHByaWNlIGlzICR7dGF4X2Ftb3VudH0nKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>When you execute the program and enter some numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter the price ($):<span class=\"hljs-number\">100<\/span>\nEnter the tax rate (%):<span class=\"hljs-number\">10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230; you&#8217;ll get the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Traceback (most recent call last):\n  File <span class=\"hljs-string\">\"main.py\"<\/span>, line <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-keyword\">in<\/span> &lt;module&gt;\n    tax_amount = price * tax \/ <span class=\"hljs-number\">100<\/span>\nTypeError: can<span class=\"hljs-string\">'t multiply sequence by non-int of type '<\/span>st<span class=\"hljs-string\">r'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the input values are strings, you cannot apply the multiply operator.<\/p>\n\n\n\n<p>To solve this issue, you need to convert the strings to numbers before performing calculations.<\/p>\n\n\n\n<p>To convert a string to a number, you use the <code>int()<\/code> function. More precisely, the <code>int()<\/code> function converts a string to an integer.<\/p>\n\n\n\n<p>The following example uses the <code>int()<\/code> function to convert the input strings to numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = input(<span class=\"hljs-string\">'Enter the price ($):'<\/span>)\ntax = input(<span class=\"hljs-string\">'Enter the tax rate (%):'<\/span>)\n\ntax_amount = int(price) * int(tax) \/ <span class=\"hljs-number\">100<\/span>\nprint(<span class=\"hljs-string\">f'The tax amount is $<span class=\"hljs-subst\">{tax_amount}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cHJpY2UgPSBpbnB1dCgnRW50ZXIgdGhlIHByaWNlICgkKTonKQp0YXggPSBpbnB1dCgnRW50ZXIgdGhlIHRheCByYXRlICglKTonKQoKdGF4X2Ftb3VudCA9IGludChwcmljZSkgKiBpbnQodGF4KSAvIDEwMApwcmludChmJ1RoZSB0YXggYW1vdW50IGlzICR7dGF4X2Ftb3VudH0nKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>If you run the program, and enter some values, you&#8217;ll see that it works correctly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter the price ($):\n<span class=\"hljs-number\">100<\/span>\nEnter the tax rate (%):\n<span class=\"hljs-number\">10<\/span>\nThe tax amount <span class=\"hljs-keyword\">is<\/span> $<span class=\"hljs-number\">10.0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='other-type-conversion-functions'>Other type conversion functions <a href=\"#other-type-conversion-functions\" class=\"anchor\" id=\"other-type-conversion-functions\" title=\"Anchor for Other type conversion functions\">#<\/a><\/h2>\n\n\n\n<p>Besides the <code>int(str)<\/code> functions, Python supports other type conversion functions. The following shows the most important ones for now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>float(str)<\/code> &#8211; convert a string to a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">floating-point number<\/a>.<\/li>\n\n\n\n<li><code>bool(val)<\/code> &#8211; convert a value to a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-boolean\/\">boolean<\/a> value, either <code>True<\/code> or <code>False<\/code>.<\/li>\n\n\n\n<li><code>str(val)<\/code> &#8211; return the string representation of a value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-the-type-of-a-value'>Getting the type of a value <a href=\"#getting-the-type-of-a-value\" class=\"anchor\" id=\"getting-the-type-of-a-value\" title=\"Anchor for Getting the type of a value\">#<\/a><\/h2>\n\n\n\n<p>To get the type of value, you use the <code>type(value)<\/code> function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">result_type = type(<span class=\"hljs-number\">100<\/span>)\nprint(result_type)\n\nresult_type = type(<span class=\"hljs-number\">2.0<\/span>)\nprint(result_type)\n\n\nresult_type = type(<span class=\"hljs-string\">'Hello'<\/span>)\nprint(result_type)\n\nresult_type = type(<span class=\"hljs-literal\">True<\/span>)\nprint(result_type)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cmVzdWx0X3R5cGUgPSB0eXBlKDEwMCkKcHJpbnQocmVzdWx0X3R5cGUpCgpyZXN1bHRfdHlwZSA9IHR5cGUoMi4wKQpwcmludChyZXN1bHRfdHlwZSkKCgpyZXN1bHRfdHlwZSA9IHR5cGUoJ0hlbGxvJykKcHJpbnQocmVzdWx0X3R5cGUpCgpyZXN1bHRfdHlwZSA9IHR5cGUoVHJ1ZSkKcHJpbnQocmVzdWx0X3R5cGUp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">int<\/span>'&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">float<\/span>'&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">str<\/span>'&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">bool<\/span>'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number <code>100<\/code> has the type of <code>int<\/code>.<\/li>\n\n\n\n<li>The number <code>2.0<\/code> has the type of <code>float<\/code>. <\/li>\n\n\n\n<li>The string <code>'Hello'<\/code> has the type of <code>str<\/code>. <\/li>\n\n\n\n<li>And the <code>True<\/code> value has the type of <code>bool<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>In front of each type, you see the <code>class<\/code> keyword. It isn&#8217;t important for now. And you&#8217;ll learn more about the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a> later. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>input()<\/code> function to get an input string from users.<\/li>\n\n\n\n<li>Use type conversion functions such as <code>int()<\/code>, <code>float()<\/code>, <code>bool()<\/code>, and <code>str(vaue)<\/code>to convert a value from one type to another.<\/li>\n\n\n\n<li>Use the <code>type()<\/code> function to get the type of a value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=type-conversion\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"162\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-type-conversion\/\"\n\t\t\t\tdata-post-title=\"Python Type Conversion\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"162\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-type-conversion\/\"\n\t\t\t\tdata-post-title=\"Python Type Conversion\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about type conversion in Python and some useful type conversion functions. Introduction to type conversion in Python # To get input from users, you use the input() function. For example: Try it When you execute this code, it&#8217;ll prompt you for input on the Terminal: If you enter a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-162","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/162\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}