DEV Community

muthukumarasamy thangavel
muthukumarasamy thangavel

Posted on

Python _ String Introdution

What is String?
->String is immutalble.
->All the languages string is spcial method.
->String in python are surrounded by either single qutation marks, or double qutation marks.
-> In Python more string methods are available.
-> String are commonly used for text handling and manipulation.

Immutable:
->Existing Memory wont get affected. Instead, change will be updated in new memory.
Mutable:
->A mutable value is one that can be changed without creating an entirely new value, in comparison with immutable values.

name = "Muthukumarasamy" #String 
print(type(name))   # its print only the which type <class 'str'>
print(id(name))    # its print memory allocation ID 
Enter fullscreen mode Exit fullscreen mode
Output:
     <class 'str'>
     2702430470704

Enter fullscreen mode Exit fullscreen mode

Garbage Collector:
->Unsed memory objects.
->Garbage Collection in Python is an automatic process that handles memory allocation and deallocation, ensuring efficient use of memory.
->In python method gc in this method import th python file
-> Garbage collection mostly used for the OTP verfication.
-> More methods are available.
-> Commomly uesed method are:

gc.enable()
Enable automatic garbage collection.
gc.disable()
Disable automatic garbage collection
gc.isenabled()
Return True if automatic collection is enabled.

Enter fullscreen mode Exit fullscreen mode
name = "Muthukumarasamy"    
print(id(name))            # its unused memory its called by the garbage collection old memory:  2639045886512
name = "T Muthukumarsamy"
print(id(name))           # new Memory allocation its a string  new memory allocation: 2639045890608
Enter fullscreen mode Exit fullscreen mode

String Length:

name = "MuthuKumarasamy"
print(len(name))      # string length calculator
i = 0
while i<len(name):
     print(name[i],end = ' ')
     i+=1
Enter fullscreen mode Exit fullscreen mode

Method overload:
->The @overload decorator allows describing functions and methods that support multiple different combinations of argument types.
->This pattern is used frequently in builtin modules and types.

Examble:

print('spam, spam, spam'.find('sp'))

print('spam, sdam, spam'.find('sp', 5))
Enter fullscreen mode Exit fullscreen mode
class calculater:
    def add (self, a , b=0):
        return a+b

calc = calculater()
print(calc.add(5)) 
print(calc.add(5,10))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)