More Variables, Strings, and Arithmetic

Variables and Types

So far, we have seen the following variable types:

  1. integer (whole number, either positive or negative)
  2. float(number with a decimal value)
  3. string(one or more characters of text)
  4. boolean (True/False)

Variables can be converted from one type to another. You should have seen this in your lab assignment:

age = float(input("Please enter your age: "))

In this instance, a string was converted to a floating point number. This allows the computer to treat this value as a number instead of text. Similarly, you can convert a floating point number to an integer using the int()function.

age = 18.5 years
rounded_age = int(age)

Similarly, a string can be converted to text using the str()function. Python easily allows you to see the type of the variable using the type()function.

String Manipulation

In this section, we explore string concatenation and indexing.

String Methods

Python provides several built-in functions for manipulating strings. You can view all of this function in the Python documentation. Some functions include upper(), which converts the entire string to uppercase, find(), which returns the index of the first matching index, and isalpha(), which returns the value True if all characters are alphabetical (no numbers, spaces, etc.). A method is a special type of function and is called differently than the functions we have seen thus far. Here are a few examples:

css.php