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

Strings

The document provides an overview of strings in Python, including their creation, manipulation, and special cases. It covers string concatenation, inserting values using format() and f-strings, and handling multiline strings with triple quotes. Additionally, it includes exercises for practicing string manipulation and formatting.

Uploaded by

allietang818
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)
5 views3 pages

Strings

The document provides an overview of strings in Python, including their creation, manipulation, and special cases. It covers string concatenation, inserting values using format() and f-strings, and handling multiline strings with triple quotes. Additionally, it includes exercises for practicing string manipulation and formatting.

Uploaded by

allietang818
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
You are on page 1/ 3

Strings

Strings in Python

1. Introduction to Strings

Strings represent words or text.

Extensively used in programming.

2. Creating Strings

Can be created using single ( ' ' ) or double ( " " ) quotation marks.

e.g. 'word' or "word"

Ensure consistency; don't mix single and double quotation marks in the
same string.

Strings offer various methods for manipulation like upper() , isnumeric() , and
strip() .

3. String Concatenation

Combine strings using the + operator.

e.g. "Hello" + " World"

4. Inserting Values into Strings

Use the format() method with placeholders {} .

e.g. "Hello, {}. You are {} years old".format(name, age)

Alternatively, use f-strings for a clearer approach.

e.g. f"Hello, {name}. You are {age} years old"

5. Multiline Strings

Use triple quotes ( ''' ''' or """ """ ) for strings spanning multiple lines.

Escape characters can also control line breaks:

\n creates a new line.

\t inserts a tab.

6. Special Cases in Strings

Strings 1
For quotes inside a string, mix single and double quotation marks or use
escape character ( \ ).

e.g. 'He said, "This is great!"' or "He said, \"This is great!\""

Use \ before special characters to treat them as normal characters.

7. Exercise: Create an f-string

Objective: Display "Hello, my name is X and my hobby is Y" .

1. Define two variables, name and hobby .

2. Construct the f-string: f"Hello, my name is {name}\nand my hobby is {hobby}" .

3. Print the resulting string.

Exercise:
1. String Manipulation
Given the string s = "PythonIsAwesome" , perform the following tasks:
a. Convert the entire string to uppercase.
b. Extract and print the substring "Is".
c. Replace the substring "Awesome" with "Fantastic" and print the result.
d. Check if the string is alphanumeric and print the result.

2. Multiline Strings and Escape Characters

You are given a string which reads: She said, "Python is fun!" .
a. Write this string in a way that the quotation marks are part of the string itself.
b. Create a multiline string that looks like this when printed:

he said,
"Python
is
fun!"

3. F-String Formatting

You have two variables:

Strings 2
name = "Alice"
age = 25

Using f-strings, create and print a string that says: Hello, my name is Alice and I'm 25
years old.

Strings 3

You might also like