Python String


What is String ?

  • String is a collection of characters.

  • Strings are amongst the most popular types in Python.

  • We can create them simply by enclosing characters in quotes.

  • Python treats single quotes the same as double quotes.

String Literals :

In python, string can be can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multi-line strings and doc-strings.

Example to create strings

name = 'Python Prowess' # using single quote

mobile = "8537480001" # using double quote

address = '''5/30 K.D.A Colony
Noida,
UP'''       # using triple quote, multi-line

print(name)
print(mobile)
print(address)
       

Each string is stored in the computer’s memory as a list of characters.
myString = "prowess"
<------indexes----->
0 1 2 3 4 5 6
p r o w e s s

Python also supports negative index

Negative indices start at the end of the string and move left.
<---- +Ve indexes --->
0 1 2 3 4 5 6
p r o w e s s
-7 -6 -5 -4 -3 -2 -1
<---- -Ve indexes --->

Accessing single characters :

We can access individual characters by using indices in square brackets.

>>> myString = "prowess"
>>> myString[0]
'p'
>>> myString[1]
'r'
>>> myString[-1]
's'
>>> myString[-2]
's'
>>> myString[7]  #  Can't access index out of range
Traceback (most recent call last):
  File "", line 1, in 
    myString[7]
IndexError: string index out of range
>>> myString[-8] #  Can't access index out of range
Traceback (most recent call last):
  File "", line 1, in 
    myString[-8]
IndexError: string index out of range
>>>

Accessing substrings :

We can access substring by using slicing operator [ : ] .

How to use [ : ] ?

Syntax

[ start_index : end_index : step ]

All the parameters are optional.

  • if we do not provide start_index,it uses default value 0.

  • if we do not provide end_index ,it uses default value lenght of string, and it will always be excluded to get the substring.

  • if we do not provide step ,it uses default step 1.

Example :

>>> myString = "prowess"
0 1 2 3 4 5 6
p r o w e s s
-7 -6 -5 -4 -3 -2 -1
>>> myString[ : ] # start=0, end=7, step=1 'prowess' >>> myString[1:4] 'row' >>> myString[::2] # start=0, end=7, step=2 'poes' >>> myString[-6:-2:] 'rowe' >>> myString[::-1] # reverse of string 'sseworp' >>>


Strings are immutable :

Strings in python is not merely a sequence of characters.

Strings are immutable type objects of str type.

>>> myString = "prowess"
>>> print(type(myString))
<class 'str'>  'str' type object
>>>         

Meaning of Immutability :-

We can’t change the sequence of Characters encapsulated in a String Object. Each operation on that object will produce a new String object.

Let's understand with example

>>> myString = "prowess"

# perform operation to change it into upper case

>>> myString.upper()
'PROWESS'

# as we can see the ouput is in upper case.
# Does it make change into existing string myString
# Let us access it and see

>>> print(myString)
prowess

# As we can see, it does not make change into 
# existing object, it creates a new str type object.
>>> 

Update / Delete String

As we know, strings are immutable, so we cannot change it once it has been assigned.

We can simply reassign different strings to the same name.

>>> myString = "prowess"
>>> myString[0] = 'P'
Traceback (most recent call last):
  File "", line 1, in 
    myString[0] = 'P'
TypeError: 'str' object does not support item assignment
>>> myString = "PythonProwess"
>>> print(myString)
PythonProwess
>>>

We cannot delete or remove characters from a string. But deleting the whole string is possible by using the keyword del.

>>> del myString[1]
Traceback (most recent call last):
  File "", line 1, in 
    del myString[1]
TypeError: 'str' object doesn't support item deletion
>>> del myString
>>> print(myString)
Traceback (most recent call last):
  File "", line 1, in 
    print(myString)
NameError: name 'myString' is not defined
>>>

Length of Strings

The len() method is use to find the length of a string.

>>> s = "Hello"
>>> x = len(s)
>>> print("length of string =",x)
length of string = 5
>>>

Concatenation of Strings

The  + operator is use to concate two or more strings in Python.

>>> s1 = "Python"
>>> s2 = " Prowess"
>>> s3 = s1 + s2
>>> print(s3)
Python Prowess #  OUTPUT
>>>

Repeatition of Strings

The  * operator is use to repeat the string for a given number of times.

>>> s = "Prowess"
>>> s1 = s * 2
>>> print(s1)
ProwessProwess  #  OUTPUT
>>> 

Iteration through String

>>> s = "Prowess"
>>> for var in s:
	print(var)
	
P
r
o
w
e
s
s
>>>

Membership Opearator

We can use membership operator in and not in to check if a sub string exists within a string or not.

>>> s = "PythonProwess"
>>> "Python" in s
True
>>> "python" not in s
True
>>> "py" in s
False
>>> "x" in s
False
>>>

Next topic is for Loop





 










Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved