# Python: String's Method

  • In this lesson, we will learn about what we can do on string.

# in

a = "HelloWorld"
print("Hello" in a)
print("he" in a)
1
2
3

OUTPUT

True False

# split

  • return the list of string which are splited.
a = "   Hello 1 2 3     5    6 7    9 10 0"
print(a.split())

b = "Hello,1,2,3,5,9"
print(b.split(","))

c = "1-2-3-4-5-6    -7"
print(c.split("-"))
1
2
3
4
5
6
7
8

OUTPUT

['Hello', '1', '2', '3', '5', '6', '7', '9', '10', '0'] ['Hello', '1', '2', '3', '5', '9'] ['1', '2', '3', '4', '5', '6 ', '7']

# replace

  • return new string which is replaced by some strings.
a = "100000000000"
print(a.replace("0",""))

b = "HelloWorld"
print(b.replace("World","Hello2"))

c = "1-2-3-4-5-6-7"
print(c.replace("-"," "))
1
2
3
4
5
6
7
8

OUTPUT

1 HelloHello2 1 2 3 4 5 6 7

# strip

  • return new string which is removed the whitespaces and tabs from head and tail.
a = "    Hello World        "
print(a.strip())
1
2

OUTPUT

Hello World