Skip to content

String

Creating

Creating

1
2
3
4
5
6
7
8
a = 'Rudra1'
print(a)

b = "Rudra2"
print(b)

c = '''Rudra3'''
print(c)
Output
1
2
3
Rudra1
Rudra2
Rudra3

Accessing Substrings

Accessing

1
2
3
4
5
# Positive Indexing
# in python counting start from the 0

a = 'Hello World' 
print(a[0])
Output
H 

1
2
3
# Negative Indexing
a = 'Hello World'
print(a[-1])
Output
d

1
2
3
4
#Slicing

a = 'Hello World'
print(a[0:6])
Output
Hello

1
2
3
4
5
# if you are use -ve indexing then the first number is > second number
    # 6 > 0

a = 'Hello World'
print(a[6:0:-2])
Output
Wol

1
2
3
# Reverse the Line

print(a[::-1])
Output
dlroW olleH

Editing & Deleting

Editing

1
2
3
4
# python strings are immutable

a = 'Hello World'
a[0] = 'k'
Output
1
2
3
4
5
1 # python strings are immutable
      3 a = 'Hello World'
----> 4 a[0] = 'k'

TypeError: 'str' object does not support item assignment

Deleting

1
2
3
4
# deleting
s = 'Hello world'
del s
s
Output
1
2
3
4
5
6
2 s = 'Hello world'
      3 del s
----> 4 print(s)
      5 s

NameError: name 's' is not defined

g = 'Hello'
del g[1:3]
Output
1
2
3
4
1 g = 'Hello'
----> 2 del g[1:3]

TypeError: 'str' object does not support item deletion

Operations

Operations

print('Hello'+' '+'world')
Output
Hello World

print('Love_you_' * 5) # five times string 
Output
Love_you_Love_you_Love_you_Love_you_Love_you_

# we compare the word in lexicographically 

'Delhi' != 'Odisha' # False
'Delhi' > 'Odisha' # False
'Delhi' and 'Odisha' # 'Odisha'
'Delhi' or 'Odisha' # 'Delhi'
'' and 'Odisha' # false and true -> false
'' or 'Odisha' # Odisha
not '' # empty is false --> True
not 'Rudra' # value is True --> False
Output


Functions

len

len('Rudra Prasad Bhuyan')
Output
19

min

min('Rudra Prasad Bhuyan')
Output
' '

max

max('Rudra Prasad Bhuyan')
Output
y

sorted

sorted('Rudra Prasad Bhuyan', reverse=True)
Output
['y',
'u',
'u',
's',
'r',
'r',
'n',
'h',
'd',
'd',
'a',
'a',
'a',
'a',
'R',
'P',
'B',
' ',
' ']

capitalize

a = 'Rudra prsad bhuyan'
a.capitalize()
Output
'Rudra prsad bhuyan'

title

a.title() # only first letter of the word is capital letter
Output
'Rudra Prsad Bhuyan'

upper

a.upper()
Output
'RUDRA PRSAD BHUYAN'

swapcase

'Rudra Prasad Bhuyan'.swapcase()
Output
'rUDRA pRASAD bHUYAN'

lower

'Rudra Prasad Bhuyan'.lower()
Output
'rudra prasad bhuyan'

count

'My name is Rudra Prasad Bhuyan'.count('a')
Output
5

index

1
2
3
4
'My name is Rudra Prasad Bhuyan'.index('Rudra') 
'My name is Rudra Prasad Bhuyan'.index('z')  # ValueError: substring not found

# showing error if hte string iss not present  
Output
11

endswith

'My name is Rudra Prasad Bhuyan'.endswith('z')
Output
False 

startswith

'My name is Rudra Prasad Bhuyan'.startswith('My')
Output
True

format

1
2
3
name = 'Rudra'
age = 20
'My name is {} and my age is {}'.format(name, age)
Output
'My name is Rudra and my age is 20'

isalnum

'Rudra_1234'.isalnum()
Output
False

isalnum

'Rudra_1234$%'.isalnum()
Output
False

title

'RUDRA'.isalpha()
Output
True

isdigit

'1234'.isdigit()
Output
True

isidentifier

'Rudra'.isidentifier()
Output
True

split

'My name is Rudra Prasad Bhuyan'.split(sep=None)
Output
['My', 'name', 'is', 'Rudra', 'Prasad', 'Bhuyan']

'My name is Rudra Prasad Bhuyan'.split('a')
Output
['My n', 'me is Rudr', ' Pr', 's', 'd Bhuy', 'n']

join

' '.join(['My', 'name', 'is', 'Rudra', 'Prasad', 'Bhuyan'])
Output
'My name is Rudra Prasad Bhuyan'

' ---'.join(['My', 'name', 'is', 'Rudra', 'Prasad', 'Bhuyan'])
Output
'My ---name ---is ---Rudra ---Prasad ---Bhuyan'

'My name is Rudra Bhuyan'.replace('Rudra', 'Nitesh')
Output
'My name is Nitesh Bhuyan'