Python Tutorial: String in Python
Showing posts with label String in Python. Show all posts
Showing posts with label String in Python. Show all posts

Friday, 14 August 2020

String in Python

What is string?

In Python, a consecutive sequence of characters, which are enclosed or surrounded by single (‘ ’) or double (“ ”) quotes, is known as string. This sequence of characters may include a letter, a number, special character or a backslash (\). Python Strings are immutable.

How to create a string in Python?

>>> str='String Tutorial'

>>> print(str)

String Tutorial

>>> str="Python Tutorial"

>>> print(str)

Python Tutorial

>>> my_string='''Work in Progress'''

>>> print(my_string)

Work in Progress

>>> my_string="""Work in Progress"""

>>> print(my_string)

Work in Progress

>>> my_string= """Work                       # triple quotes string can extend multiple lines

in

Progress"""

>>> print(my_string)

Work

in

Progress

>>> my_string="Tutorial on \"Python\" "  # using escape sequence \”

>>> print(my_string)

Tutorial on "Python"

>>> str1="Accessing string characters in new line\n Python"     # using escape sequence \”

>>> print(str1)

Accessing string characters in new line

 Python

>>> str1="Chapter :\t String"

>>> print(str1)

Chapter:      String

 

How to access characters in a string? Or Traversing a string

For example :

A= “COMPUTER”

We can start Index value either from left or from right. Left index starts from 0 and right-side index start from -1

For Example:

Str1= “PYTHON STRING”

 STR="Informatics"

for i in STR:

          print(i)

 

output:      

I

n

f

o

r

m

a

t

i

c

s

 

Special String operators

String can be manipulated using operators like concatenate (+), repetition (*), and membership operator like in and not in.

 

 

•          Slicing

 

Example :

Str1= “PYTHON STRING”

>>> Str1="PYTHON STRING"

>>> Str1[1:10]

'YTHON STR'

>>> Str1[::]

'PYTHON STRING'

>>> Str1[0:13:2]

'PTO TIG'

>>> Str1[-1:13:2]

'G'

>>> Str1[::-1]

'GNIRTS NOHTYP'

>>> Str1[2:-13:-1]

'TY'

>>> Str1[-2:-13:-1]

'NIRTS NOHTY'

>>> Str1="PYTHON"

>>> Str1[::2]

'PTO'

>>> Str1[:]

'PYTHON'

>>> Str1[-2:]

'ON'

>>> Str1[:-2]

'PYTH'

  •   Updating:

Syntax :

          String[range]+ ‘x’

Where  range[start, stop, step]

>>> Str1="PYTHON STRING"

>>> Str1[0:7]+'JYTHON'

'PYTHON JYTHON'

>>> Str1="PYTHON STRING"

>>> "LIST"+Str1[0:7]

'LISTPYTHON '

  •   Concatenation: The “+” operator joins or concatenates strings written on both sides of the operator and creates a new string.

Example :

>>> "Welcome "+"Python"

'Welcome Python'

>>> s="Language "+"Python"

>>> print(s)

Language Python

  • Replicating/ Repetition: The * operator creates a new string by replicating multiple copies of the same string

Example:

>>> S="Python"

>>> S*3

'PythonPythonPython'

>>> "Jython "*4

'Jython Jython Jython Jython '

•          Membership Operators: 

  • in: Returns True if a character or substring exists in the given string, False otherwise
  • not in: Returns True if a character or substring does not exist in the given string, False otherwise

Example:

>>> s="Language Python"

>>> 'l' in s

False

>>> 'l' not in s

True

>>> 'P' in "International"

False

>>> 't' in "International"

True

>>> 't' not in "International"

False

>>> 'm' not in "International"

True