Monday, April 3, 2023

Strings in Python

String is an immutable data structure in Java. That means, once value has been assigned to a String, it can’t be changed and if changed, a new object is created. It is also a sequence of characters. Python does not have a character data type, a single character is simply a string with a length of 1. 

Create

String str = "Test"

String str = 'Test'

String str = '''Tes

t'''

Retrieve

String characters can be retrieved with the index.

s = str[3] # fourth character

s = str[-2] # second last character

s = str[1:3] # slicing, returns characters from 1 to 2

i = str.index('e') # returns index

str.index('e', 2, 7) # start and stop locations

str.rindex('e')  # last index


Add/Update/Delete

str = "Hello".join(str)  # using join

str = "Hello" + str[0] + 'eeeee' + str[1:]  #  using plus  # indexing and slicing

str += "hi" 

str = str.replace('l','m')

str = str[0:4]   # deletion

del str

str[0] = 'x' will give error


In all the three cases, the result has to be reassigned to the value, else value will return unchanged.


Formatting

str = r"The \n stands for next line"    #  raw string

str = "{} {} {}".format('I', 'like', 'mango')

str = "{0} {2} {1}".format('I', 'mango', 'like')

var="mango"

str = f"I like {var}"

num = 12.3456789

print('Two decimals: %3.2f' %num)

print('Four decimals: %3.4f' %num)

print(a,b)  # concat

str1 = ' '

str2 = 'geeks'

print(repr(str1 and str2))  # returns ' '   # repr is printable representation to print quotes

print('a' and 'b')  # returns b or second element for and

print('a' or 'b')  # returns a or first element for or

'a' * 3 # returns aaa


Operations

Other operations we can do with a String are,

len(str),  str.upper(), str.strip() 

str.split(',')   # returns list

't' in str, str.find("t"),   str.index("t")

str[2:10:2]   # start,stop,step

str[::-1]   # to reverse

print(type(str)) is String

for x in "banana":

  print(x)


str(obj)  # type casting

ord(str) to get ascii

chr(str) to get back string from ascii

str[len(str)-1]

str.isalnum()   # alphanumeric


Comment inside a method given at the top with triple quotes is called DocStrings.


Memory

str="check" and str1="check" are saved in the same memory location.

str==str1 checks for value and returns true.

str.__eq__(str1) also checks for value and returns true.

__eq__() method can be overridden in a class.

str is str1 checks for memory location and returns true


(In Java, == checks for location and .equals() of String is overridden to check for value. Location is same within Stringpool.)


Now, str="check@" and str1="check@" are saved in different memory locations due to special character.

So str==str1 is true, str.__eq__(str1) is true, but str is str1 is false.

String objects are saved in the heap just like any other object. But String interning is a process that reuses basic alphanumeric Strings. This saves a lot of memory.

No comments: