Loading...

Python Strings


in this tutorial we are going to learn about strings in python. how to create strings, access strings, update strings and delete strings in python

What is a String in python ?


a string is a collection of bytes (group of unicode chars) which collectively forms a letter, name, word, sentence etc.
in python strings are used to store information which is in the form of alphabets, characters, words etc.
string represents the information which human reads and understands easily instead of binaries 0's and 1's which computer understands.


Create Strings in python


in python we create strings by enclosing single quote ( ' ' ) or by enclosing double quote ( " " ) around it and then assign it to a variable.
for example:
        		     					    		
# variable with single quote

x = 'ciaz'

# variable with double quote

y = "car"

print(f"{x} is a  {y}")

output:

ciaz is a car

        		     					    		
        		     					    	


Access Chars in Strings


Positive indexing in python if you want to access a character in specific position in the string then we use indexing.
index starts from 0, 1, 2 and so on.. for example: access fourth char in string
        		     					    		
x = 'football'

# remember index position starts from 0 
# so the fourth char will be in index 3.

print(x[3])

output:

t

        		     					    		
        		     					    	
Negative indexing
in python the negative indexing is use for accessing the index position in reverse order
which means index -1 represents the last position, -2 represents the second last position so on..
for example: access the last char in string
        		     					    		
x = 'smarty'

print(x[-1])

output:

y

        		     					    		
        		     					    	
Slicing
in python the slicing is used for access the range of chars in the string
which means it is used for accessing a sub string (part of a string) from the given string.
the slicing uses the start index position and end index position which is seperated by colon ( [start : end] )
for example: access the last char in string
        		     					    		
x = 'football'

print(x[4:7])

output:

bal

        		     					    		
        		     					    	
in the above example chars from index position 4 to index position 6 is accessed
because in range 4:7, here it means start from 4 and end upto 7 (means 6)


Update Strings in python


in python, strings are immutable which means once we create or initialize a string it can not be change or update
but we can reassign the whole string in it.
change
for example: if we try to update a char in string
        		     					    		
x = 'football'

x[0] = 'p'

print(x)

output:

Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
TypeError: 'str' object does not support item assignment

        		     					    		
        		     					    	
in the above program when we try to change a char in string it raise an error.

re-assign
for example:
        		     					    		
x = 'football'

print(x)

x = 'volleyball'

print(x)

output:

football
volleyball

        		     					    		
        		     					    	


Delete Strings in python


since strings are immutable we can not delete or remove chars from string
but we can delete the string by using the del keyword
delete char from string in python
for example: if we try to delete a char in string
        		     					    		
x = 'football'

del x[0]

print(x)

output:

Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
TypeError: 'str' object doesn't support item deletion
        		     					    		
        		     					    	
in the above program when we try to delete a char in string it raise an error.

delete string in python
for example:
        		     					    		
x = 'football'

del x

print(x)

output:

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
NameError: name 'x' is not defined

        		     					    		
        		     					    	
in the above program the string is delete from the memory
now the variable becomes undefined.