Menu Close
Python String Tutorial

In this python string tutorial, you will learn about Python string and some important methods. Python string is a sequence of characters. In this guide, we will see how to create, update, and delete strings using appropriate examples.

In the previous tutorial, we have seen all about Python lists and their methods.

What is Python String?

Python String is a collection of characters or string is a sequence of characters. A character is simply a symbol. The English language has 26 characters and using those characters a string is formed.


In python, the string is defined within single quotation marks or double quotation marks. Python provides various string methods that are useful to work with python strings.
for example, we create a string like “Python” or ‘Python‘.

Create a Python string:

In Python, the string can be created by enclosing characters inside single quotes or double quotes.

Example


str1 = "Programming Funda"
print(str1)
str2 = 'Programming Funda'
print(str2)

Output

Programming Funda
Programming Funda

The data type of the Python string

To get the data type of the Python string you need to use the type() function.

Example: Getting data type of the string


str1 = "Programming"
print(str1)
print(type(str1))
str2 = 'Programming'
print(str2)
print(type(str2))

Output

Programming
<class 'str'>
Programming
<class 'str'>

Multiline Python String

Sometimes you have a lot of string or multiline string then you can use three double quotation marks or three single quotation marks.

Example


a = """Python is a 
very powerful programming 
language"""
print(a)

b = '''
Python is used to build a web application,
desktop application,
GUI Application and 
so on.'''
print(b)

Output

Python is a
very powerful programming
language

Python is used to build a web application,
desktop application,
GUI Application and
so on.

Access String Characters

a. To access the specific character of a string using the index number of a particular character.

string1 = "Programming"
print(string1[3]]

The output will be:- g

b. You can use a negative index to access the string character.

string2 = "Programming Funda"
print(string2[-3])

The output will be:- n

c. Sometimes you need access to characters within a specific range then you can concept of slicing.
In slicing, you specify two indexes first is the start index and the second is the end index separated by a colon (:).

x = 'programming Funda'
print(x[2:6])

The output will be:- ogra

Python String Length

To calculate the length of the string, use len() built-in function.

x = 'programming Funda'
print(len(x))

The output will be:- 17

Change or Delete a Python String

String in Python is immutable which means you can not change or delete a python string. If you try this you will get an error.


x = 'programming Funda'
x[2] = 'b'

The output will be:-

Traceback (most recent call last):
  File "C:\Users\user1\Desktop\file\test.py", line 141, in <module>
    x[2] = 'b'
TypeError: 'str' object does not support item assignment

Iterate a Python string

To loop through a string use, you can use Python for loop.


x = "Python"
for i in x:
	print(i)

Output

P
y
t
h
o
n

Delete Python String

To delete a complete string use the del keyword.


x = "Programming"
del x
print(x)

Python String Methods

String in Python provides a lot of string methods which is used to work with string.

  • To change the lowercase, use the lower() method.
a = 'PROGRAMMING'
print(a.lower())
  • To change the uppercase, use the upper() method.
a = 'programming'
print(a.upper())
  • To remove white spaces from the beginning and end of the string use strip() method.
x = " Python "
print(x.strip())
  • To check string is lowercase or not use the islower() method.
x = 'programming'
print(x.islower())
  • To check string is upper case or not use the isupper() method.
x = 'PYTHON'
print(x.isupper())
  • To convert the first character of the string in upper case use capitalize() method.
x = 'programming'
print(x.capitalize())

Add Two Python String

To add or concatenate two strings use plus (+) operator.

x = 'Django'
y = ' Framework'
result = x + y
print(result)

The output will be:- Django Framework

Python String Format

Sometimes we need to add numbers with strings. earlier we learned, we can not add or concatenate strings and numbers together.
if we try this then we will get an error.


salary = 40000
string = "my name is john and my salary is " + salary
print(string)

Output

Traceback (most recent call last):
  File "C:\Users\user1\Desktop\file\test.py", line 141, in <module>
    string = "my name is john and my salary is " + salary
TypeError: can only concatenate str (not "int") to str

To insert a variable value inside a string use format() method. format() take a variable name or passed argument and places them inside a string where {} placeholders are.

Example

salary = 40000
string = "My name is john and my salary is ${}".format(salary)
print(string)

Output

My name is John and my salary is $40000

Escape Character

if you insert a string that is surrounded by double quotes inside another string.
If you do this, you will get an error.


profile = "My name is "Vishvajit" and i am "20" years old"
print(profile)

To fix this problem you need to use a backslash (\) to escape this (“) character.


profile = "My name is \"Vishvajit\" and I am \"20\" years old"
print(profile)

Output

My name is "Vishvajit" and I am "20" years old

Here are some important escape characters that can be used in Python.

  • \’ – Single quotes
  • \ – Backslash
  • \n – New line
  • \r – Carrieg Return
  • \t – tab
  • \b – backspace
  • \f – form
  • \ooo – octal value
  • \xhh – Hexa value

String Methods

Conclusion:

In this Python string tutorial, you have learned about Python string and how to perform operations using string. String in Python provides lots of string built-in methods that are useful when you will work with string in Python.


In the above string methods list, We listed all the string methods, You can read by clicking on particular methods.

I hope this Python string article will help you. If this article will help you please share it with your friends who want to learn Python programming.

For More Information:- Click Here

Comprehension in Python
How to Limit Decimal Places in Python

Related Posts