Menu Close
Python String Tutorial

Python String Tutorial

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

In previous tutorial we have seen all about Python list and its 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. 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 string.
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

Data type of Python string

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

Example

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 string use the index number of a particular character.

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

Output will be:- g

b. You can use negative index for access the string character.

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

Output will be:- n

c. Sometimes you need access 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])

Output will be:- ogra

Python String Length

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

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

Output will be:- 17

Change or Delete a Python String

String in Python is immutable that 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'

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 completely string use del keyword.

x = "Programming"
del x
print(x)

Python String Methods

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

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

Add Two Python String

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

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

Output will be:- Django Framework

Python String Format

Sometimes we need to add numbers with string. earlier we learned, we can not add or concatenate string and number 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 variable value inside a string use format() method. format() take a variable name or passed argument and place 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 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 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 with your friends who want to learn Python programming.

For More Information:- Click Here

Previous Article
Next Article

Related Posts