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.
Headings of Contents
- 1 What is Python String?
- 2 Create a Python string:
- 3 The data type of the Python string
- 4 Multiline Python String
- 5 Access String Characters
- 6 Python String Length
- 7 Change or Delete a Python String
- 8 Iterate a Python string
- 9 Delete Python String
- 10 Python String Methods
- 11 Add Two Python String
- 12 Python String Format
- 13 Escape Character
- 14 String Methods
- 15 Conclusion:
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
- String capitalize() method
- String casefold() method
- String center() method
- String count() method
- String endswith() method
- String expandtabs() method
- String find() method
- String format() method
- String index() method
- String rjust() method
- String swapcase() method
- String startswith() method
- String ljust() method
- String strip() method
- String isalpha() method
- String isalnum() method
- String isdecimal() method
- String isdigit() method
- String identifier() method
- String islower() method
- String isupper() method
- String isnumeric() method
- String printable() method
- String maketrans() method
- String translate() method
- String splitlines() method
- String title() method
- String encode() method
- String isspace() method
- String istitle() method
- String join() method
- String lower() method
- String lstrip() method
- String partition() method
- String replace() method
- String rindex() method
- String rpartition() method
- String rfind() method
- String rsplit() method
- String rstrip() method
- String upper() method
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