Variables and Methods

#!/bin/python3
  • Shebang line: runs the script using Python 3.


Variables and Methods

quotes = "All is fair in love & war."
  • A string variable named quotes is 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 word
print(len(quotes))        # Prints the length (number of characters) of the string

Working with Other Data Types

name = "KT"               # A string variable
age = 25                  # An integer variable
gpa = 2.8                 # A float (decimal) variable
print(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 length

  • Demonstrates variable types and type conversion (int(), str())

  • Demonstrates variable update with +=

Last updated