04

Day 4: Strings and String Methods

Chapter 4 • Beginner

35 min

Strings in Python are arrays of bytes representing Unicode characters. Python does not have a character data type, a single character is simply a string with a length of 1.

String Properties:

  • Immutable (cannot be changed after creation)
  • Can be enclosed in single or double quotes
  • Support escape characters
  • Can be concatenated and repeated

Common String Methods:

  • len() - Get string length
  • upper() - Convert to uppercase
  • lower() - Convert to lowercase
  • strip() - Remove whitespace
  • split() - Split string into list
  • replace() - Replace characters
  • find() - Find substring
  • format() - Format strings

Hands-on Examples

String Basics

# String creation
name = "Python Programming"
message = 'Hello, World!'
multiline = """This is a
multiline string"""

print("Name:", name)
print("Message:", message)
print("Multiline:", multiline)

# String length
print("Length of name:", len(name))

# String indexing
print("First character:", name[0])
print("Last character:", name[-1])
print("Substring:", name[0:6])

Strings are indexed starting from 0. Negative indexing starts from the end.