String In Python

 


Strings

  • String is a collection of characters and can be defined as 
    • in single quotes   '  '
    • in double quotes  "  "
    • in single-triple quotes  '''  '''
    • in double-triple quotes   """   """

#program

name = 'Techno Xpresss'
Website = "salahtutorials.com"
description = '''SALAH : Sit And Learn At Home'''
slogan= """Self Study is a Key 2 success"""

print(name)
print("type(name) : ", type(name))
print(Website)
print("type(name) : ", type(Website))
print(description)
print("type(name) : ", type(description))
print(slogan)
print("type(name) : ", type(slogan))


#output

Techno Xpresss
type(name) : <class 'str'>
salahtutorials.com
type(name) : <class 'str'>
SALAH : Sit And Learn At Home
type(name) : <class 'str'>
Self Study is a Key 2 success
type(name) : <class 'str'>

                    double-triple quotes are generally used for documentation


  • To access individual element of String, indexing is used.

#program

name = "Techno Xpresss"

print("name[0] : ", name[0])
print("name[5] : ", name[5])

#output

name[0] : T
name[
5] : o


  • String allows positive and negative indexing.

#program

name = "Techno Xpresss"

print("name[0] : ", name[0])
print("name[5] : ", name[5])
print("name[-1] : ", name[-1])
print("name[-6] : ", name[-6])

#output

name[0] : T
name[
5] : o
name[-
1] : s
name[-
6] : p


  • String can be defined as Empty

#program

name = ""

print("name : ", name)
print("type(name) : ", type(name))

#output

name :
type(name) : <
class 'str'>


  • String with single element can also be defined

#program

name = "T"

print("name : ", name)
print("type(name) : ", type(name))

#output

name : T
type(name) : <
class 'str'>


            remember, a single/individual character will also be considered as a String in Python



#program

name = 'Techno Xpresss'


print("name[ : : ] : ", name[ : : ])
print("name[ : :-1] : ", name[ : :-1])
print("name[ : :2] : ", name[ : :2])
print("name[2: :2] : ", name[2: :2])

#output

name[ : : ] : Techno Xpresss
name[ : :-
1] : ssserpX onhceT
name[ : :
2] : Tcn pes
name[
2: :2] : cn pes


  • String allows multiplication and addition, while doing so, it will return new string.

#program

f_name = "Techno"
l_name = "Xpresss"

print("f_name : ", f_name)
print("l_name : ", l_name)
print("f_name + l_name : ", f_name + l_name)
print("f_name + l_name : ", f_name + " " + l_name)
print("f_name*3 + l_name : ", f_name * 3 + l_name)
print("(f_name* + l_name )*3: ", (f_name + l_name + " ")*3)


#output

f_name : Techno
l_name : Xpresss
f_name + l_name : TechnoXpresss
f_name + l_name : Techno Xpresss
f_name*
3 + l_name : TechnoTechnoTechnoXpresss
(f_name* + l_name )*
3: TechnoXpresss TechnoXpresss TechnoXpresss


  • Modify members value with assignment operator is not allowed

#program

name = "Techno Xpresss"
print("name : ", name)
name[
6] = '-'
print("name : ", name)


#output

name : Techno Xpresss
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 5, in <module>
name[6] = '-'
TypeError: 'str' object does not support item assignment












Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India