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)
print("My name is " + name + " and I am " + str(age) + " years old.")
# Combines strings and variables using concatenation
# str(age) converts the integer to a string so it can be joined

Incrementing a Variable

age += 1                  # Adds 1 to age → now age = 26
print(age)                # Prints the new value of age

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 +=

#!/bin/python3

#Variables and Methods
quotes = "All is fair in love & war."
print(quotes)
print(quotes.upper())
print(quotes.lower())
print(quotes.title())

print(len(quotes))


name = "KT"
age = 25
gpa = 2.8

print(int(age))
print(int(gpa))

print("My name is " + name + " and I am " + str(age) + " years old.")

age += 1
print(age)

Last updated