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"
0 1 2 3 4 5 6 p r o w e s s
Python also supports negative indexNegative indices start at the end of the string and move left.
<---- +Ve indexes --->
<---- -Ve indexes --->
0 1 2 3 4 5 6 p r o w e s s -7 -6 -5 -4 -3 -2 -1
Accessing single characters :
We can access individual characters by using indices in square brackets.
>>> myString = "prowess" >>> myString 'p' >>> myString 'r' >>> myString[-1] 's' >>> myString[-2] 's' >>> myString # Can't access index out of range Traceback (most recent call last): File "
", line 1, in myString 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.
>>> myString = "prowess"
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 = 'P' Traceback (most recent call last): File "
", line 1, in myString = '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 myString Traceback (most recent call last): File "
", line 1, in del myString 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
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
+ 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
* 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 >>>
We can use membership operator
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
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at email@example.com
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at firstname.lastname@example.org