
Hello Techies,
In this blog, we are learning about String in Python with Example. Especially, I am covering more topics like create a string, how to access characters in the string, concatenation of two or more strings, and so on with the help of examples.
Below are the points that we are covering in this blog:
- Python String(Introduction)
- Create String in Python
- Access character in the String
- How to Change a String in Python
- How to Delete a String in Python
- Concatenation of Two or More Strings
- Loop through a String
- Python String Methods
Table of Contents
#1. Python String(Introduction)
- A string is a group of characters.
- Strings are enclosed in double-quotes or single quotes.
- The str data type represents the String.
- Example: “Hello”, “TechPlusLifeStyle”, “Test”
#2. Create String in Python
The string can be enclosed in double-quotes or single quotes. Even Triple quotes can be used in Python but are most often used to represent multiline strings and Docstring.
Example:
data = "TechPlusLifestyle"
print(data)
data1 = 'TechPlusLifestyle'
print(data1)
data2 = '''TechPlusLifestyle'''
print(data2)
data3 = """Hello, welcome to
the world of TechPlusLifeStyle"""
print(data3)
# Using Escape Character
data4 = "Hello Techies, \nHow are you?"
print(data4)
Output is:
TechPlusLifestyle
TechPlusLifestyle
TechPlusLifestyle
Hello, welcome to
the world of TechPlusLifeStyle
Hello Techies, How are you?
#3. Access character in the String
We can use indexing to access individual characters and slicing for a range of characters. The index starts from 0 onwards and is written inside square braces.
String Indexing in Python
This index is represented in 2 ways Positive and negative index. The Positive index starts from the 0 positions while in Negative indexing -1 refers to the last element, -2 refers to the second last element, and so on.
Example for Positive Indexing
data = "test"
print(data[0])
print(data[1])
print(data[2])
print(data[3])
Output is:
t
e
s
t
Negative Indexing
data = "test"
print(data[-4])
print(data[-3])
print(data[-2])
print(data[-1])
Output is:
t
e
s
t
If you try to access an out-of-range index or use numbers other than integers, we will receive errors.
Example:
>>> data = "test" >>> print(data[8]) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> print(data[1.5]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: string indices must be integers
String slicing in Python
Slice Operator: Using (colon) we can enter the range of items in the list.
Example:
data = "test"
print(data[1:4])
print(data[2:-1])
Output:
est
s
#4. How to Change a String in Python
Strings are immutable, meaning we cannot change the value once assigned. We can reassign different strings to the same name.
Example
data = 'test'
data[2] = r
data = 'test1'
print(data)
Output is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module> NameError: name 'r' is not defined
test1
#5. How to Delete a String in Python
Deleting a String completely, however, is possible using the del keyword, but we can’t delete elements from String, because String is immutable so we can’t change String’s elements.
Example:
data = 'test'
del data[1]
del data
print(data)
Output is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module> TypeError: 'str' object doesn't support item deletion
Traceback (most recent call last):
File "<stdin>", line 1, in <module> NameError: name 'data' is not defined
#6. Concatenation of Two or More Strings
Concatenation is the adding of two or more strings in one. To add a variable to another variable, use the + operator. The * operator can be used to repeat the string for a given number.
Example:
str1 = 'Python'
str2 ='Developer'
# + operator
print(str1 +' '+ str2)
# * operator
print(str1 * 3)
Output
Python Developer
PythonPythonPython
#7. Loop through a String
Using the loop we can iterate through the string. Here is an example iterate through a String.
for each in 'test':
print(each)
Output is:
t e s t
#8. Python String Methods
The methods available with the string objects in Python programming are in the table below.
Method Name | Description |
---|---|
format() | Formats the values specified in the string |
lower() | Convert string to lowercase |
upper() | Convert string to Uppercase |
join() | Joins the items of an iterable at the end of the string |
split() | Splits the string to the specified separator and returns the list |
find() | Finds the string for the specified value and returns it to the position where it was found |
replace() | Returns the string where a specified value is replaced with the specified value |
strip() | Returns a trimmed string |
Example:
data = "Test"
data.lower()
data.upper()
data.find('t')
'test'.replace('te', 'test1')
Output
'test'
'TEST'
3
'test1st'
I hope you understand the use of “String in Python“. Still, you have any doubts please comment below.
Check out the official site for more details on “String in Python”.
