Python Keywords, Identifiers and Statements 12

strip():
The strip() method removes any whitespace from the beginning or the end:

a = ” Hello, World! “
print(a.strip()) # returns “Hello, World!”
#output:
Hello, World
(spaces are removed from both sides)

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
#output:
Hello, World

len():
The len() method returns the length of a string:

a = “Hello, World”
print(len(a))
output:
12
(space within the string is included in the length)

a = "Hello, World"
print(len(a))
#output:
12

lower():
The lower() method returns the string in lower case:

a = “Hello, World!”
print(a.lower())
#output:
hello, world!

a = "Hello, World!"
print(a.lower())
#output:
hello, world!

upper():
The upper() method returns the string in upper case:

a = “Hello, World!”
print(a.upper())
#output:
HELLO, WORLD!

a = "Hello, World!"
print(a.upper())
#output:
HELLO, WORLD!

replace():
The replace() method replaces a string with another string:

a = “Hello, World!”
print(a.replace(“H”, “J”))
#output:
Jello, World

a = "Hello, World!"
print(a.replace("H", "J"))
#output:
Jello, World

split():
The split() method splits the string into substrings if it finds instances of the separator:

a = “Hello, World!”
print(a.split(“,”)) # returns [‘Hello’, ‘ World!’]
>>> a = “Hello, World!”
>>> print(a.split(“,”))
#output:
[‘Hello’, ‘ World!’]

or

>>> a=”Hello, World!”
>>> b=a.split(“,”)
>>> print(b)
#Output:
[‘Hello’, ‘ World!’]

a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
>>> a = "Hello, World!"
>>> print(a.split(","))
#output:
['Hello', ' World!']
>>> a="Hello, World!"
>>> b=a.split(",")
>>> print(b)
#Output:
['Hello', ' World!']