Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: Cannot modify header information - headers already sent by (output started at /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/ekhaliya/public_html/topcse/wp-content/plugins/sg-cachepress/core/Supercacher/Supercacher_Helper.php on line 77


In previous tutorial we have learnt about operators and in this tutorial we are going to learn about strings , how we create it, and what operations we can perform on it .


string is a sequence of characters which is treated as a single data item. Python strings are array of characters or any group of characters defined between double quotes or single quotes.

therefore ,so For example, “string” or ‘string’.

The characters in the string are stored in the consecutive memory location as the string is treated as an array of characters. Each character occupies 1 byte in the memory heap.

Python strings are also immutable, meaning they cannot be modified once created.

Creation of Strings

Creating strings in Python is as simple as assigning a value to the variable. The value that is being assigned must be in either within double quotes ” ” or single quotes ‘ ‘ for the string and For multiple lines triple quotes are used as well .

>>>str =’hello’






>>>str2=””” hi , how are you . …………………

……………………what are you doing “””


>>>hi , how are you . …………………

……………………what are you doing

concatenation of strings 

Concatenation means joining two or more strings together.In Python, this is achieved by using  + operator.



>>>print (a+b)

>>>hi nams


>>>print (x)


calculating the length of a string

Python has  len(string)  function which  returns length of a string

>>>str=”hi ram”



Accessing characters in strings

In Python, characters or elements are accessed using indexing and slicing .

  • Indexing : it means locating position of char .






  •  Slicing : basically it is  retrieving a set of values from a particular sequence.In slicing we have str[startpoint : endpoint : stepsize] , here Step size is by default 1, if not mentioned .





>>>  print (str)





>>>print(str[ : ])




Iteration in strings

It is done using loops .



>>>for i in str:








Python built-in methods for string manipulation

  • all( str) : It returns true if all the elements in the iterable are true.
  • any(str ):It returns true if any element in the iterable are true.
  • isdecimal( ):Returns true if a string contains only decimal characters .
  • zfill (width):It returns a string padded with 0’s, width being the length of string padded with 0’s.
  • upper( ):Converts lowercase letters to uppercase in the string.
  • lower( ):Converts uppercase letters to lowercase in the string.
  • swapcase( ):It reverses uppercase into lowercase characters and vice-versa.
  • strip([chars]):It performs both lstrip() and rstrip() on the given string.
  • startswith(str):It checks whether the string starts with character ‘str’.
  • split(str):It splits the string from the left.
  • rstrip( ):It removes all trailing characters or white space of the string.
  • rjust(width):It returns right-justified string of given width.
  • rindex( str):It is same as index( ), but search backwards in the string.

some of the more inbuilt functions are :

  • rfind(str, start,end):It returns highest index of the sub-string.
  • replace(old, new):It replaces ‘old’ sub-string with the ‘new’ string.
  • min(str):Returns the min alphabet from the string str.
  • max(str):It returns the max alphabet from string ‘str’.
  • lstrip( ):Removes all leading characters or white spaces in the string.
  • ljust(width):Returns a left-justified string of given width.
  • len(str):Returns the length of the string ‘str’.
  • join(seq):Returns concatenated string representations of elements in sequence ‘seq’.
  • isupper( ):Returns true if string has uppercase letters.
  • istitle( ):Checks if a string is properly titlecased or not. Return true if titlecased.
  • isspace( ):It is used to check white-space in the string.
  • isnumeric( ):Returns true if the string contains only numeric digits.
  • islower( ):Returns true if string has lowercase characters.
  • isdigit( ):Checks if string contains only digits.
  • isalpha( ):Returns true therefore, if string has alphanumeric character.
  • isalnum( ):Returns true so if string has at least 1 character and all characters are alphanumeric and false otherwise.
  • index(str):Returns the index of sub-string ‘str’ but raises an exception if ‘str’ not found.
  • find(str):Determine if ‘str’ is present in string or not.
  • endswith(suffix):Checks if the string ends with specified character or suffix.
  • count(m):Counts how many times m occurs in a string.
  • center( ):Returns a space-padded string with the specified character.
  • capitalize( ):It capitalizes the first letter of the string.
  • ascii(str ):It returns printable version of string ‘str’.

string formatting  

  • ESCAPE CHARACTERS:it is non printable character written as “\”.strings escape sequence



%d bloggers like this: