Foundations of Python

93 / 134

String methods

An str object just like other objects in Python has 2 components-
1) data (string itself), and
2) methods associated with it i.e. built-in functions associated with any instance of the object.

a = "cloudxlab"
dir(a)

The dir function lists the methods associated with the object. Try it in the notebook.

To get information regarding any method, you can use the help function.

help(a.translate)

It provides simple documentation on the translate method of str object. You can also write as,

help(str.translate)

We call a method just like the function call but here the variable or the instance of the object is followed by a . and then the method name. For eg,

print(a.upper())

It prints the string in uppercase as CLOUDXLAB. Now, if we do

print(a)

Can you tell the output

Further string methods' detailed descriptions you can find here.



Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...