This article will be having snippets for the purpose of better understanding.

Strings are used in Python to record text information, such as names. Strings in Python are actually a sequence, which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string “hello’ to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).

This idea of a sequence is an important one in Python and we will touch upon it later on in the future.

In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes. While this might sometimes look different from the input (the enclosing quotes could change), the two strings are equivalent. The string is enclosed in double-quotes if the string contains a single quote and no double quotes, otherwise, it is enclosed in single quotes. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:

The string can be created in Python using either Single Quotes ‘ ’ or Double Quotes “ ”

‘Hello World’


“Hello World”

Caution should be taken while using quotes.

‘ I’m using single quotes, but this will create an error’


 invalid syntax

These will give a syntax error once you run.

This is because the single quote I’m stopped the string.

In [] : "Now I'm ready to use the single quotes inside a string!"

String Slicing and Indexing.

In[1] : "Now I'm ready to use the single quotes inside a string!"Out[1] :  "Now I'm ready to use the single quotes inside a string!"In [2]: my_string= "Now I'm ready to use the single quotes inside a string!"In[3]: my_string[0:14]out[3]:"Now I'm ready "

For better understanding can be done by trying these code line

HelloWorld without space between Hello & World

H e l l o W o r l d

0 1 2 3 4 5 6 7 8 9

H takes the value or 0 in Python. Every first word it considered as 0 in Python





… & so on


H e l l o W o r l d

10 -9 -8 -7 -6 -5 -4 -3 -2 -1

these are reverse string.

Can you guess if:-

Hello World with space between Hello & World

Let’s start indexing!

For s=’Hello World’

In[1] :# Show first element (in this case a letter)



 [2]:s[1]Out[2]:'e'In[3] :print(s[2])Out[3]:1

We can use a : to perform slicing which grabs everything up to a designated point. For example:

In[1] :# Grab everything past the first term all the way to the    length of s which is len(s)


      # Note that there is no change to the original 

       sOut[2}:'Hello World'In[3]:# Grab everything UP TO the 3rd index


#Note the above slicing. Here we’re telling Python to grab everything from 0 up to 3. It doesn’t include the 3rd index. You’ll notice this a lot in Python, where statements and are usually in the context of “up to, but not including”.

In[1] :#Everything

       s[:]Out[1]:'Hello World'We can also use negative indexing to go backwards.

In[2]:# Last letter (one index behind 0 so it loops back around)



In[3]:# Grab everything but the last letter


Out[3]:'Hello Worl'

We can also use index and slice notation to grab elements of a sequence by a specified step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements. For example:

In[1] :# Grab everything, but go in steps size of 1


Out[1]:'Hello World'

In[2]:# Grab everything, but go in step sizes of 2



# Upper Case a string

# Lower case


String Methods

Here are some of the most common string methods. A method is like a function, but it runs “on” an object. If the variable s is a string, then the code s.lower() runs the lower() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object-Oriented Programming, OOP). Here are some of the most common string methods:

  • s.lower(), s.upper() — returns the lowercase or uppercase version of the string
  • s.strip() — returns a string with whitespace removed from the start and end
  • s.isalpha()/s.isdigit()/s.isspace()… — tests if all the string chars are in the various character classes
  • s.startswith(‘other’), s.endswith(‘other’) — tests if the string starts or ends with the given other string
  • s.find(‘other’) — searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
  • s.replace(‘old’, ‘new’) — returns a string where all occurrences of ‘old’ have been replaced by ‘new’
  • s.split(‘delim’) — returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it’s just text. ‘aaa,bbb,ccc’.split(‘,’) -> [‘aaa’, ‘bbb’, ‘ccc’]. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
  • s.join(list) — opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. ‘ — -’.join([‘aaa’, ‘bbb’, ‘ccc’]) -> aaa — -bbb — -ccc

Python does not have a separate character type. Instead, an expression like s[8] returns a string-length-1 containing the character. With that string-length-1, the operators ==, <=, … all work as you would expect, so mostly you don’t need to know that Python does not have a separate scalar “char” type.