Strings
Strings
Strings in Python
1. Introduction to Strings
2. Creating Strings
Can be created using single ( ' ' ) or double ( " " ) quotation marks.
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
5. Multiline Strings
Use triple quotes ( ''' ''' or """ """ ) for strings spanning multiple lines.
\t inserts a tab.
Strings 1
For quotes inside a string, mix single and double quotation marks or use
escape character ( \ ).
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.
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
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