Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (84 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)

String Function In Python: How To Use It with Examples

Last updated on Jul 07,2022 5.2K Views

33 / 62 Blog from Python Fundamentals

This article will tell you what is string in Python function and brief you on all that is there to know about the concept of Python String function in detail. Following pointers will be covered in this article,

SO let us get started then,

What Is String In Python?

Many of us who are familiar with programming languages like C, C++ etc. will have an answer like “String is a collection or array of characters.”

Well in Python also, we say the same definition for String data type. Python String is array of sequenced characters and is written inside single quotes, double quotes or triple quotes. Also, Python doesn’t have character data type, so when we write ‘a’, it is taken as a string with length 1.

Moving on with this article on what is String Function in Python?

How to create a string?

s = 'Hello'
s1 = "Hello"
s2 = ''' Hello
How is the <span style="font-weight: 400;" data-mce-style="font-weight: 400;">weather</span> today? '''


How is the weather today?

Triple quotes are used usually when we have both single and double quotes in the string and also when we want to write multi-line sentences.


We need to take care that while using single quotes, the string should not include single quote in it because if that happens, Python will assume that the line is finished with the occurrence of the second quote itself and desired output will not be fetched. Same notation should be followed with double quotes and triple quotes also.

Lets move into this Python String function Article now 

How to access a character from a string?

Suppose we want to access a character from a string, let’s say the last character, we need to know its position in the string.

Image- Python String Function - Edureka

Here is a string along with the position allocated. So if we want to access ‘n’ from the string, we have to go to the 5th position to have it.

The numbering or indexing starts from 0 to one less than the length of string.

Here is a python program to make us more clear in that.

str = 'Antarctica is really cold.'
print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])


str = Antarctica is really cold.
str[0] = A
str[-1] = .
str[1:5] = ntar
str[5:-2] = ctica is really col

Now if left to right follows an increasing order pattern in indexing, right to left follows a decreasing order pattern i.e. from -1, -2, -3 so on. So if you want to access the last character, you can do it in two ways.

str = 'Antarctica is really cold.'
a = len(str)
print('length of str ', a)
#last character with the help of length of the string
print('str[a] ', str[a-1])
#last character with the help of indexing
print('str[-1] ',str[-1])


length of str 26
str[a] .
str[-1] .

Python Strings are immutable in nature which implies that once a string has been declared, any character in it cannot be changed.

s = "Hello Batman"
s[2] = 'P'


Hello Batman
Traceback (most recent call last):
File “C:/Users/”, line 3, in
s[2] = ‘P’
TypeError: ‘str’ object does not support item assignment

Process finished with exit code 1

However you can delete the whole string with del operator.

s = "Hello Batman"
del s


Hello Batman
Traceback (most recent call last):
File “C:/Users/”, line 4, in
NameError: name ‘s’ is not defined

Process finished with exit code 1

If you don’t want s to be “Hello Batman” and want it to be some other string, then you can just update the string as a whole.

s = "Hello Batman"
s = "Hello Spiderman"


Hello Batman
Hello Spiderman

Moving on with this article on what is String in Python?

Formatting a String:

Formatting a string means to allocate the string dynamically wherever you want.

Python String can be formatted with the use of format() method which is very versatile and powerful tool for formatting of Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.

String1 = "{} {} {}".format('Hello', 'to', 'Batman')
print("Default order: ")
# Positional Formatting
String1 = "{1} {0} {2}".format('Hello', 'to', 'Batman')
print("nPositional order: ")
# Keyword Formatting
String1 = "{c} {b} {a}".format(a='Hello', b='to', c='Spiderman')
print("nString in order of Keywords: ")
# Formatting of Integers
String1 = "{0:b}".format(20)
print("binary representation of 20 is ")
# Formatting of Floats
String1 = "{0:e}".format(188.996)
print("nExponent representation of 188.996 is ")
# Rounding off Integers
String1 = "{0:.2f}".format(1 / 6)
print("none-sixth is : ")
# String alignment
String1 = "|{:&lt;10}|{:^10}|{:&gt;10}|".format('Hello', 'to', 'Tyra')
print("nLeft, centre and right alignment with Formatting: ")


Default order:
Hello to Batman

Positional order:
to Hello Batman

String in order of Keywords:
Spiderman to Hello

Binary representation of 20 is 10100

Exponent representation of 188.996 is

one-sixth is :

Left, center and right alignment with Formatting:
|Hello | to | Tyra|

A string can be left (<), right(>) or center (^) justified with format method.

{:<10}.format(“Hello”) means Python will reserve 10 space for the string and the string will start from the left. The same goes with right and center justification.

I hope that you learnt the Python String Function concepts well and hence try it out to be more accurate.

This brings us to the end of this article on What Is String In Python? Don’t be in the sidelines when that happens, to master your skills enroll in Edureka’s python certification program and become a leader.

Have any questions? Mention them in the comments. We will get back to you as soon as possible.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 20th July,2024

20th July

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 24th August,2024

24th August

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

String Function In Python: How To Use It with Examples