- Escape sequences
- Extra mini tips for the basics
- Additional Practices
What I learnt:
1. Escape sequences
Escape sequences allow us to include special characters in strings.
We can add a backslash \ before the character we want to escape.
- ‘ apostrophe within single quotes
sentence = 'Hey, that\'s my bag.' print(sentence)
Output:
Hey, that's my bag.
- ’’ ‘‘ quotes within quotes
sentence = "They are the so-called \"cool kids\" in school." print(sentence)
Output:
They are the so-called "cool kids" in school.
- \n to create a new line and generate multi-line strings.
print("Hello this is\na random\nstring.")
Output:
Hello this is a random string.
- \t tab
print("Hello this is\n\ta random\n\t\tstring.")
Output:
Hello this is a random string.
- \ backslash character
print("C:\\Users\\Me\\Desktop")
Output:
C:\Users\Me\Desktop
- Raw strings treats backslash \ as a literal character.
A raw string can be used by prefixing the string with r or R, allowing backslashes without needing to escape them.
But unescaped backslashes at the end of a raw string will cause an error.print(r"Backslashes \ don't need to be escaped in raw strings.")
Output:
Backslashes \ don't need to be escaped in raw strings.
2. Extra mini tips for the basics
-
input( ) function always gives a string value by default
- We can duplicate the same string multiple times, using *
string = 'Ann' print(string*3)
Output:
AnnAnnAnn
- Combining variables/ values:
- comma , gives a space between values
- meanwhile, + is concatenate and doesn’t give a space between outputs
- + will not work if using different data types e.g. string + integer
string1='hello' print(string1,'Joe') print(string1+'Joe')
Output:
hello Joe helloJoe
- + will not work if using different data types e.g. string + integer
- can use f strings to concatenate as well f”{ } { }”
firstname = "Joe" lastname = "Tan" print(f"{firstname} {lastname}")
Output:
Joe Tan
- use ” “ not “” for one spacing
Additional Practices
- Sorting items into lists
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", "intermediate cuneiform", "medial cuneiform"] longer_names = [] shorter_names = [] for bone_name in foot_bones: if len(bone_name) < 10: shorter_names.append(bone_name) else: longer_names.append(bone_name) print(shorter_names) print(longer_names)
Output:
['calcaneus', 'talus', 'cuboid', 'navicular'] ['lateral cuneiform', 'intermediate cuneiform', 'medial cuneiform']
- Sorting items in lists into strings
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", "intermediate cuneiform", "medial cuneiform"] longer_names = "" shorter_names = "" for bone_name in foot_bones: if len(bone_name) < 10: shorter_names += "\n" + bone_name else: longer_names += "\n" + bone_name print(shorter_names) print(longer_names)
Output:
calcaneus talus cuboid navicular lateral cuneiform intermediate cuneiform medial cuneiform
- Using \n and \t escape characters
print("Twinkle, twinkle, little star, \n\tHow I wonder what you are! \n\t\tUp above the world so high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star, \n\tHow I wonder what you are!")
Output:
Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are!