- immutable
''' are used to create docstrings, and multilne strings
- indexing works the same as lists
s1 = 'Hello'
print(s1)
print(type(s1))
# Hello
# <class 'str'>
s = '''
Hey, may name is Afzal
I am 27 years old.
I work as a Project Manager at Coding Ninjas.
'''
print(s)
# Hey, may name is Afzal
# I am 27 years old.
# I work as a Project Manager at Coding Ninjas.
String concatenation
String Methods
Replicate
Slicing
Comparing Strings
- Similar to lists
- Unicode value is compared
- object ID of the strings depends on the platform
- in our system, doesn’t matter if the variable is different, object id stays the same since they have the same contents
str1 = "Ninja"
str2 = "Ninja"
str3 = str1
print("ID of str1 =", hex(id(str1)))
print("ID of str2 =", hex(id(str2)))
print("ID of str3 =", hex(id(str3)))
print(str1 is str1)
print(str1 is str2)
print(str1 is str3)
# ID of str1 = 0x7fe6b544fdf0
# ID of str2 = 0x7fe6b544fdf0
# ID of str3 = 0x7fe6b544fdf0
# True
# True
# True
Conversion
chr() returns a character for ASCII value
ord() returns the ASCII value for character
Questions on slicing
Question on Comparison
Iterating on strings