{"id":734,"date":"2020-02-27T09:27:51","date_gmt":"2020-02-27T17:27:51","guid":{"rendered":"https:\/\/renanmf.com\/?p=734"},"modified":"2020-12-29T10:27:08","modified_gmt":"2020-12-29T13:27:08","slug":"type-casting-python","status":"publish","type":"post","link":"https:\/\/renanmf.com\/type-casting-python\/","title":{"rendered":"Type casting in Python"},"content":{"rendered":"<h2>Explicit conversion<\/h2>\n<p>To cast a variable to a string just use the <code>str()<\/code> function.<\/p>\n<pre><code class=\"language-python\">my_str = str(&#039;32&#039;) # this is just a regular explicit intialization\nprint(my_str)\n\nmy_str = str(32) # int to str\nprint(my_str)\n\nmy_str = str(32.0) # float to str\nprint(my_str)<\/code><\/pre>\n<p>To cast a variable to an integer just use the <code>int()<\/code> function.<\/p>\n<pre><code class=\"language-python\">my_int = int(32) # this is just a regular explicit intialization\nprint(my_int) #output: 32\n\nmy_int = int(3.2) # float to int: rounds down to 3\nprint(my_int) #output: 3\n\nmy_int = int(&#039;32&#039;) # str to int\nprint(my_int) #output: 32<\/code><\/pre>\n<p>To cast a variable to a float just use the <code>float()<\/code> function.<\/p>\n<pre><code class=\"language-python\">my_float = float(3.2)   # this is just a regular explicit intialization\nprint(my_float) #output: 3.2\n\nmy_float = float(32)     # int to float\nprint(my_float) #output: 32.0\n\nmy_float = float(&#039;32&#039;)  # str to float\nprint(my_float) #output: 32.0<\/code><\/pre>\n<p>What I did before is called <em>explicit<\/em> type conversion.<\/p>\n<p>In some cases you don&#8217;t need to do the conversion explicitly, Python can do it by itself.<\/p>\n<h2>Implicit conversion<\/h2>\n<p>The example below shows the implicit conversion when adding an <code>int<\/code> and a <code>float<\/code>.<\/p>\n<p>Notice that <code>my_sum<\/code> is a float. Python uses <code>float<\/code> to avoid data loss since the <code>int<\/code> type can not represent the decimal digits.<\/p>\n<pre><code class=\"language-python\">my_int = 32\nmy_float = 3.2\n\nmy_sum = my_int + my_float\n\nprint(my_sum) # output: 35.2\n\nprint(type(my_sum)) # &lt;class &#039;float&#039;&gt;<\/code><\/pre>\n<p>On the other hand, in this example, when you add an <code>int<\/code> and a <code>str<\/code>, Python will not be able to make the implicit conversion, and the explicit type conversion is necessary.<\/p>\n<pre><code class=\"language-python\">my_int = 32\nmy_str = &#039;32&#039;\n\nmy_sum = my_int + int(my_str) # explicit conversion works\nprint(my_sum)# output: 64\n\nmy_sum = my_int + my_str # implicit conversion throws an error\n#output:\n#Traceback (most recent call last):\n#  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\n#TypeError: unsupported operand type(s) for +: &#039;int&#039; and &#039;str&#039;<\/code><\/pre>\n<p>The same error is thrown when trying to add <code>float<\/code> and <code>str<\/code> types without making the explicit conversion.<\/p>\n<pre><code class=\"language-python\">my_float = 3.2\nmy_str = &#039;32&#039;\n\nmy_sum = my_float + float(my_str) # explicit conversion works\nprint(my_sum)# output: 35.2\n\nmy_sum = my_float + my_str # implicit conversion throws an error\n#output:\n#Traceback (most recent call last):\n#  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\n#TypeError: unsupported operand type(s) for +: &#039;float&#039; and &#039;str&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Explicit conversion To cast a variable to a string just use the str() function. my_str = str(&#039;32&#039;) # this is just a regular explicit intialization print(my_str) my_str = str(32) # int to str print(my_str) my_str = str(32.0) # float to str print(my_str) To cast a variable to an integer just use the int() function. my_int [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":742,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6],"class_list":["post-734","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Type casting in Python<\/title>\n<meta name=\"description\" content=\"Learn how type casting works in Python and avoid problems when dealing with different types of data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/renanmf.com\/type-casting-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Type casting in Python\" \/>\n<meta property=\"og:description\" content=\"Learn how type casting works in Python and avoid problems when dealing with different types of data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/renanmf.com\/type-casting-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Renan Moura - Software Engineering\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/renanmouraf\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-27T17:27:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T13:27:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Renan Moura\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/renanmouraf\" \/>\n<meta name=\"twitter:site\" content=\"@renanmouraf\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Renan Moura\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/\"},\"author\":{\"name\":\"Renan Moura\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\"},\"headline\":\"Type casting in Python\",\"datePublished\":\"2020-02-27T17:27:51+00:00\",\"dateModified\":\"2020-12-29T13:27:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/\"},\"wordCount\":146,\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/\",\"name\":\"Type casting in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"datePublished\":\"2020-02-27T17:27:51+00:00\",\"dateModified\":\"2020-12-29T13:27:08+00:00\",\"description\":\"Learn how type casting works in Python and avoid problems when dealing with different types of data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"width\":1800,\"height\":1200,\"caption\":\"how to python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/type-casting-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/renanmf.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Type casting in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"name\":\"Renan Moura - Software Engineering\",\"description\":\"Software development, machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/renanmf.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\",\"name\":\"Renan Moura\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"width\":120,\"height\":120,\"caption\":\"Renan Moura\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/renanmouraf\",\"https:\\\/\\\/x.com\\\/renanmouraf\",\"https:\\\/\\\/instagram.com\\\/renanmouraf\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\",\"name\":\"Renan Moura\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"caption\":\"Renan Moura\"},\"description\":\"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.\",\"sameAs\":[\"https:\\\/\\\/renanmf.com\\\/\",\"https:\\\/\\\/www.instagram.com\\\/renanmouraf\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/renanmouraf\"],\"url\":\"https:\\\/\\\/renanmf.com\\\/author\\\/renanmoura\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Type casting in Python","description":"Learn how type casting works in Python and avoid problems when dealing with different types of data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/renanmf.com\/type-casting-python\/","og_locale":"en_US","og_type":"article","og_title":"Type casting in Python","og_description":"Learn how type casting works in Python and avoid problems when dealing with different types of data.","og_url":"https:\/\/renanmf.com\/type-casting-python\/","og_site_name":"Renan Moura - Software Engineering","article_publisher":"https:\/\/www.facebook.com\/renanmouraf","article_published_time":"2020-02-27T17:27:51+00:00","article_modified_time":"2020-12-29T13:27:08+00:00","og_image":[{"width":1800,"height":1200,"url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","type":"image\/jpeg"}],"author":"Renan Moura","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/renanmouraf","twitter_site":"@renanmouraf","twitter_misc":{"Written by":"Renan Moura","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/renanmf.com\/type-casting-python\/#article","isPartOf":{"@id":"https:\/\/renanmf.com\/type-casting-python\/"},"author":{"name":"Renan Moura","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8"},"headline":"Type casting in Python","datePublished":"2020-02-27T17:27:51+00:00","dateModified":"2020-12-29T13:27:08+00:00","mainEntityOfPage":{"@id":"https:\/\/renanmf.com\/type-casting-python\/"},"wordCount":146,"publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"image":{"@id":"https:\/\/renanmf.com\/type-casting-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/renanmf.com\/type-casting-python\/","url":"https:\/\/renanmf.com\/type-casting-python\/","name":"Type casting in Python","isPartOf":{"@id":"https:\/\/renanmf.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/renanmf.com\/type-casting-python\/#primaryimage"},"image":{"@id":"https:\/\/renanmf.com\/type-casting-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","datePublished":"2020-02-27T17:27:51+00:00","dateModified":"2020-12-29T13:27:08+00:00","description":"Learn how type casting works in Python and avoid problems when dealing with different types of data.","breadcrumb":{"@id":"https:\/\/renanmf.com\/type-casting-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/renanmf.com\/type-casting-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/type-casting-python\/#primaryimage","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","width":1800,"height":1200,"caption":"how to python"},{"@type":"BreadcrumbList","@id":"https:\/\/renanmf.com\/type-casting-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/renanmf.com\/"},{"@type":"ListItem","position":2,"name":"Type casting in Python"}]},{"@type":"WebSite","@id":"https:\/\/renanmf.com\/#website","url":"https:\/\/renanmf.com\/","name":"Renan Moura - Software Engineering","description":"Software development, machine learning","publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/renanmf.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/renanmf.com\/#organization","name":"Renan Moura","url":"https:\/\/renanmf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","width":120,"height":120,"caption":"Renan Moura"},"image":{"@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/renanmouraf","https:\/\/x.com\/renanmouraf","https:\/\/instagram.com\/renanmouraf","https:\/\/www.linkedin.com\/in\/renanmouraf\/"]},{"@type":"Person","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8","name":"Renan Moura","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","caption":"Renan Moura"},"description":"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.","sameAs":["https:\/\/renanmf.com\/","https:\/\/www.instagram.com\/renanmouraf\/","https:\/\/www.linkedin.com\/in\/renanmouraf\/","https:\/\/x.com\/https:\/\/twitter.com\/renanmouraf"],"url":"https:\/\/renanmf.com\/author\/renanmoura\/"}]}},"_links":{"self":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/comments?post=734"}],"version-history":[{"count":2,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":2681,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/734\/revisions\/2681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media\/742"}],"wp:attachment":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media?parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/categories?post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/tags?post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}