DEV Community

Cover image for String DataType in Python
Ezhil Abinaya K
Ezhil Abinaya K

Posted on

String DataType in Python

String
Strings are sequence of characters written inside quotes. It can include letters, numbers, symbols and spaces. Python does not have a separate character type.Strings are immutable.
Immutable means changes are updated in new memory. Existing memory won't affect. Mutable means existing memory can change.
Garbage Collection
Garbage collector will automatically removed unused object from memory.

name="Ezhil"
print(name)
name="K Ezhil"
print(name) #here, garbage collecor is happened.
Enter fullscreen mode Exit fullscreen mode
name="Ezhil"
print(type(name))
print(id(name))
name="K Ezhil"
print(id(name))
O/P:
<class 'str'>
134810544205488
134810544206304
Enter fullscreen mode Exit fullscreen mode
print(hash(name))
O/P:
print(hash(name))
Enter fullscreen mode Exit fullscreen mode
name="Ezhil Abinaya"
print(len(name))
i=0
while i<len(name):
      print(name[i], end='')
      i+=1
print()
O/P:
13
Ezhil Abinaya
Enter fullscreen mode Exit fullscreen mode
name="Ezhil"
print(name[0])
print(name[1])
print(name[-1])
print(name[-2])
o/p:
E
z
l
i
Enter fullscreen mode Exit fullscreen mode
name="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print(name[0:26]) #slicing operator
print(name[0:26:3]) #[start:stop:step]
print(name[:]) #all print
print(name[::2])
print(name[5:10])
O/P:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ADGJMPSVY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ACEGIKMOQSUWY
FGHIJ
Enter fullscreen mode Exit fullscreen mode
alpha="madam"
print(alpha[::-1]==alpha) # reverse a palindrome
O/P:
True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)