Variables and Methods
#!/bin/python3Shebang line: runs the script using Python 3.
Variables and Methods
quotes = "All is fair in love & war."A string variable named
quotesis created.
print(quotes) # Prints the original string
print(quotes.upper()) # Converts to uppercase
print(quotes.lower()) # Converts to lowercase
print(quotes.title()) # Capitalizes the first letter of each wordprint(len(quotes)) # Prints the length (number of characters) of the stringWorking with Other Data Types
name = "KT" # A string variable
age = 25 # An integer variable
gpa = 2.8 # A float (decimal) variableprint(int(age)) # Prints the integer value of age (still 25)
print(int(gpa)) # Converts GPA from float to integer (2)Incrementing a Variable
β Summary:
Shows string methods:
.upper(),.lower(),.title()Uses
len()to get string lengthDemonstrates variable types and type conversion (
int(),str())Demonstrates variable update with
+=


Last updated