Python: Strings, Integers, and Floats

 Hello and Welcome to Blog #2, Python: Strings and Integers.

The first blog, about the print statement, covered how to print strings and integers. But what are exactly strings and integers? Also, what are floats? That is what you will learn in this post.


Integers:

The definition of an integer in Python, or any other programming language, is the same as it is in Math. Integers are whole numbers. They can be positive or negative, and they can have an unlimited length. Integers cannot include decimals. 10 is an integer, and so is 1000, but 200.5 is not an integer, because it has a decimal point, which means it is not a whole number.


Strings:

Strings are a series of characters enclosed in double or single quotations, it does not matter which one, as long as they both are the same one (you cannot do "blank' or 'blank"). The length of the string can be as long as you want, as long as all the characters are inside the quotations. "Hello World!" is a valid string, and 'Hello World!' is also a valid string, but 'Hello World is not, because the ending quotation mark is not there. If there was an ending single quotation after World, then it would be valid.


Floats:

Floats are decimal numbers, positive or negative. They can be any length, but it is not a float if it does not have a decimal point. The difference between a float and integer is the decimal point. 2 is an integer, whereas 2.0 is a float. As with Math, there can only be one decimal point. 1.5 and 2.3 are both proper float numbers, whereas 71 and 4.5.6 are not.



So now you know what Integers, Strings, and Floats are, but how do you convert each of them to the others?


Converting an Integer or Float to a String:

Say you have the number, 10, and you want to represent it a a string. To do this, we must call upon a built-in function, str(). A function is basically a section of code that has a specific use. When we use the function str(), we give it an input between the parentheses, and it outputs the string form of the input. Here is an example:

Function String:

Input: 5

Output: str(5), or '5'


To call, or use the str() function, you simply type, str(**whatever you want to convert to a string**) Inputing a string into the function will not change the input.



Converting a String to an Integer or Float:

When converting from a String to an Integer or Float, the input must be the string form of an integer or float. To convert to an integer, the int() function is used, with the input going between the parentheses. To convert to a float, the float() function is used, also with input going between the parentheses. The input in either if these when converting a string must be made up of only the string form of an integer or float, like '2.0,' or '5.' Letters and other symbols cannot be converted to an integer or float, or only an error will be returned. For example:

Valid:

int('2')

Input:

'2'

Output:

2


float('5.2')

Input:

'5.2'

Output:

5.2


Invalid:

int('Hello World')

Input:

'Hello World'

Output:

Error Message


float('Hi. What are you doing?')

Input:

'Hi. What are you doing?'

Output:

Error Message



Converting from Integer to Float or Float to Integer:

When converting from an integer to a float and vice-versa, you simply need to type float() or int() respectively. When you convert integer to float, you put an integer into the int() function, and it output the integer as a float by adding .0 at the end. 

When converting from a float to an integer, you need to input a float number into the int() function. This will then output the integer form of the float by removing the decimal places and leaving only whatever is on the left of the decimal point.



Thank you for reading Python: String, Integer, and Floats. Please look out for more posts, and leave any comments, questions, or suggestions in the comments section.




Comments

  1. Very nice!

    I like how you've given positive and negative examples for people understand the concept better. For example, how to convert string "5.2" to float, but also how you can't convert "hello world" to float.

    There is a lot of clarity. You understand the concepts well and explain them clearly. Good job!

    ReplyDelete

Post a Comment