top of page

One Stop for Python: Strings with Examples

Python is a general-purpose programming language used in just about any kind of software you can think of. You can use it to build websites, artificial intelligence, servers, business software, and more.

Photo by Hitesh Choudhary on Unsplash

why do need python?

When we look into the advantages of python

  • simplicity: Python's English-like syntax makes it easy to learn and read, and unlike some other languages, it uses simple line breaks

  • you can execute and test your code right after writing it without an intermediate compilation process.

  • Python is a portable, cross-platform language — meaning you can write and execute Python code on any operating system with a Python interpreter.

Applications of python in real world.

  • Data science: analytics and visualization:For Data science ,analyst use python for manipulating data for reporting, predictive analysis, and more.

  • Web development: websites and web apps:Python is also widely used for back-end development, where it runs in the server of web applications and interacts with databases and APIs after a user requests data from the app's front-end in the browser.

  • Desktop Applications:Python to build desktop applications

In this blog ,I will be covering the topic of strings from Python. Before digging deep into Python ,we must build the basics first.Eventhough ,you feel like the string is easy with the theory side ...when we face coding problems in won't be very easy.So let's first understand String.


For practise section you can use online jupyter notebook,it is free .I have added link :

  1. Choose "Try it in your browser"

2. Select Jupyter lab

3. Once the Jupyter Notebook is opened, click on the newtab.Then choose Python Notebook.

Hope you guys will practice the examples...


Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). I will be doing a detailed study on the topic strings. If you are a beginner to this topic, this contains all the main points about the strings in python.

  • A string is a sequence of characters enclosed within single quotes or double quotes.

Single Quoted Strings s1='Ammu’

Double Quoted Strings s1="Ammu Fredy"

Triple Quoted Strings: By using single quotes or double quotes we cannot represent multi line string literals. so, Triple Quotations are invented.

· 🡪 Triple single quotes for multiline string

S1=''’Ammu went to

Shop to buy Fruits '''

🡪 Triple double quotes for multiline string

S1=""" Ammu went to

Shop to buy """

  • we can convert a integer to string by using str() .

In the below picture you can see that using str() int is convereted to string.type() can be used to see the datatype.

  • Strings in Python are arrays of bytes representing unicode characters.

So we can access the elements of a string using [],let’s practically see it.

  • So the first code will get the first element from the string, There is no char datatype in python. Everything is a string with a length of 1

When we printed print(p[0]) the element in the place 0 th index is displayed.

  • Slicing property of String





string starting index will be 0,index can be + or –.positive index means left to right and ,negative index means the right to left.

P[0:n] when we use this code to display , elements in the index 0 to n-1 will be displayed.

So if we want to display ‘Hai’ :p[0:3] has to be used. Now let's practically see how this slicing works

A.How to reverse a string using these slicing

B.How to check if ‘to ‘ is present in the string

C.How to display the string by each element

I have given two sets of codes ..,you guys try this and tell me which gives the correct answer in the comment .In the second set of code,I have used the function' range '.

syntax: range(start,stop,set) ,it is very important to know the range function.

In the next few problems ,let’s practise a problem that use uses range functions.

D.print the elements in odd and even index

String Functions:

below are Important functions we can use on strings,I have included examples with it for easy understanding.

  • capitilize():Converts the first character to upper case

  • casefold():Converts string into lower case

  • center():Returns a centered string, character)

Eg: p=' Hai Ammu welcome to Hawai 'needed for the returned string

  • count():Returns the number of times a specified value occurs in a string

p. count('a')ai Ammu welcome to Hawai '


O/P:3 (case sensitive so ‘A’ not considered)

Eg: p='Hai Ammu welcome to Hawai '


  • endswith():Returns true if the string ends with the specified value

Eg: p='Hai Ammu welcome to Hawai.'



  • find():Searches the string for a specified value and returns the position of where it was found

Eg: p='Hai Ammu welcome to Hawai.'



first position of ‘a’ is shown

Eg: p='Hai Ammu welcome to Hawai.'



  • format(): Formats specified values in a string

Eg :p="my name is {},I have a pet {}".format("Ammu","dog")


O/P:my name is Ammu,I have a pet dog

{} are placeholder

  • index():Searches the string for a specified value and returns the position of where it was found

Eg: p='Hai Ammu welcome to Hawai'



  • isalnum():Returns True if all characters in the string are alphanumeric

  • isalpha():Returns True if all characters in the string are in the alphabet

  • isspace():Returns True if all characters in the string are whitespaces

  • istitle():Returns True if the string follows the rules of a title

  • isupper():Returns True if all characters in the string are upper case

  • join():Converts the elements of an iterable into a string

Eg: p=('ammu',"20 year old ,hhhhhaaaa i wish ")




O/P:ammu is 20 year old,hhhhhaaaa I wish

  • lower():Converts a string into lower case

  • lstrip():Returns a left trim version of the string,if there is space in the left side of string it will be removed.

  • maketrans():Returns a translation table to be used in translation

  • partition():Returns a tuple where the string is parted into three parts

Eg :p='Hai Ammu welcome to Hawai'



O/P:(‘Hai Ammu’,’Welcome’,’to Hawai’)

1 - everything before the "match" 2 - the "match" 3 - everything after the "match"

  • replace():Returns a string where a specified value is replaced with a specified value

Eg: p='Hai Ammu welcome to Hawai'



O/p:’Hai Ammu pleasego to Hawai’

  • rfind():Searches the string for a specified value and returns the last position of where it was found

  • split():Splits the string at the specified separator, and returns a list

Eg: p='Hai Ammu welcome to Hawai'

y=p.split(' ')



  • startswith():Returns true if the string starts with the specified value

  • strip():Returns a trimmed version of the string

Eg: p='Hai Ammu welcome to Hawai '


O/p: 'Hai Ammu welcome to Hawai'

  • swapcase()Swaps cases, lower case becomes upper case and vice versa

Eg: p='Hai Ammu welcome to Hawai'



  • title():Converts the first character of each word to upper case

Eg:p=”this is my home”


O/P:This Is My Home

  • translate():Returns a translated string,


table:dictionary or mappint table tell about the detail of translate

Eg: p = "Hai ammu!" s = p.maketrans("a", "l") print(p.translate(s))

O/P:Hli lmmu!

maketrans()is used to create mapping table

Eg: q = {83:81}

p = "Hello Sam!"


O/P: Hello Qam!

  • upper():Converts a string into upper case

Eg:p=”hi ammu”



  • zfill():Fills the string with a specified number of 0 values at the beginning

Eg: p = "9"

y = p.zfill(5)



Zeros will be added to the front of number ,until it reaches specified length mentioned in the()

Now let’s practice more problems on the topic

1.Check if a string is palindrome

2.How to remove a particular word from string

3.How to remove the ith element in the string