0% found this document useful (0 votes)
4 views3 pages

String

The document provides an overview of string theory in Python, detailing string operations such as concatenation, repetition, indexing, and slicing, along with escape sequences and string methods. It also includes an exercise on string manipulation with examples of creating and formatting strings. Key concepts highlighted include string immutability and various formatting techniques like f-strings and str.format().

Uploaded by

fxcbv05928
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

String

The document provides an overview of string theory in Python, detailing string operations such as concatenation, repetition, indexing, and slicing, along with escape sequences and string methods. It also includes an exercise on string manipulation with examples of creating and formatting strings. Key concepts highlighted include string immutability and various formatting techniques like f-strings and str.format().

Uploaded by

fxcbv05928
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/21/25, 12:16 PM OneNote

Strings
21 April 2025 22:44

Theory: Strings are sequences of characters enclosed in single quotes


('), double quotes ("), or triple quotes (''' or """).
String Operations:
• Concatenation: Using + operator
• Repetition: Using * operator
• Indexing: Accessing characters by position
• Slicing: Accessing a range of characters
String Immutability: Strings in Python are immutable, meaning once
created, their content cannot be changed.
Escape Sequences:
• \n: Newline
• \t: Tab
• \\: Backslash
• \': Single quote
• \": Double quote
• \r: Carriage return
• \b: Backspace
Formatted Strings:
• f-strings: f"Value: {variable}"
• [Link](): "Value: {}".format(variable)
• %-formatting: "Value: %s" % variable
String Methods:
• upper(): Convert to uppercase
• lower(): Convert to lowercase
• strip(): Remove whitespace
• replace(): Replace substrings
• split(): Split string into list
• join(): Join list elements into string
• find(): Find substring position
• count(): Count occurrences of substring

Conver

Type Conversion

Exercise 6: String Manipulation

python
# 1. Create a string with your full name
# 2. Print your initials using indexing
# 3. Create a multiline string with your favorite quote
# 4. Use string formatting to create a sentence with variables
# 5. Use three different string methods on your name
Solution:

python
# 1. Full name string
full_name = "John Alexander Smith"
[Link] 1/3
11/21/25, 12:16 PM OneNote

# 2. Print initials
initials = full_name[0] + full_name[full_name.find("Alexander")] +
full_name[full_name.find("Smith")]
print(f"Initials: {initials}") # JAS

# 3. Multiline quote
quote = """To be or not to be,
that is the question.
- William Shakespeare"""
print(quote)

# 4. String formatting
age = 25
occupation = "developer"
formatted_str = f"My name is {full_name}, I am {age} years old,
and I work as a {occupation}."
print(formatted_str)

# 5. String methods
print(full_name.upper()) # JOHN ALEXANDER SMITH
print(full_name.lower()) # john alexander smith
print(full_name.replace("John", "Johnny")) # Johnny Alexander
Smith

Immutability

[Link] 2/3
11/21/25, 12:16 PM OneNote

[Link] 3/3

You might also like