Python strings play a very important role in working with text and data manipulation. In this blog post, I will explain to you the Python strings.
What is a string?
A string is a sequence of characters enclosed within single (”) or double (“”) quotes. It represents textual data and can include letters, numbers, symbols, and spaces. For example, “Hello, World!” and ‘12345’ are strings.
How to create a string
Example
print("This is very simple string")
Output:
This is very simple string
Set a Variable to a String
You can create a string by assigning a value within quotes to a variable.
In the provided code, two variables a
and b
are defined with the values “First string” and “Second string”, respectively.
a = "First string"
b = "Second string"
print(a)
print(b)
# or you can print two strings.
print(a, '&', b)
Here, the print
function is used to display the values of a
, “&” (a string literal), and b
, separated by a space.
The output will be:
First string
Second string
First string & Second string
Multiline Strings
Multiline strings in Python allow you to define strings that span across multiple lines. They are useful when you need to work with large blocks of text or preserve the formatting of the text. There are a few ways to create multiline strings in Python:
Using triple quotes (”’ ”’):
a = '''This is a multiline string.
This is the second line.
This is the third line.
'''
print(a)
Triple double quotes (“”” “””):
multiline_string = """
This is another multiline string.
It can also span across multiple lines.
Triple double quotes can be used as well.
"""
Using backslash () to continue the string on the next line:
a = "This is a multiline string. \
It continues on the next line using a backslash."
print(a)
Output:
This is a multiline string. It continues on the next line using a backslash.
Operations in String
String operations in Python allow you to manipulate and work with strings in various ways. Here are some example string operations:
String Concatenation
You can concatenate two or more strings using the +
operator.
In the given code, four variables str1, str2, str3, and str4 are defined with the values “Hello”, “I”, “am”, and “Aman” respectively.
str1 = "Hello"
str2 = "I"
str3 = "am"
str4 = "Aman"
result = str1 + str2 + str3 + str4
print(result)
the +
operator is used to concatenate the strings together. The result
variable stores the concatenation of str1
, str2
, str3
, and str4
. The resulting string will be “HelloIAmAman”.
Output:
HelloIamAman
String Repetition
You can repeat a string multiple times using the *
operator.
str1 = "Hello"
result = str1 * 5
print(result)
Output:
HelloHelloHelloHelloHello
String Length
You can find the length of a string using the len()
function.
str1 = "Tell me the Length of this string"
length = len(str1)
print(length)
#or you can directly print the lenth
print(len(str1))
Output:
33
33
String Indexing
You can access individual characters in a string using indexing. Indexing starts at 0 for the first character.
str1 = "Hello"
char = str1[0]
print(str1[0])
#or
print(char)
Output
H
H
String Slicing
You can extract a portion of a string using slicing. Slicing allows you to specify a range of indices to extract a substring.
Slicing Syntax:
string[start:end:step]
Example of Slicing
1. Extracting a substring:
string = "Shitus is a Free learning Blogs" substring = string[7:17] print(substring)
Output:
is a Free
2. Extracting a substring from the beginning of the string:
string = "Shitus is a Free learning Blogs" substring = string[:6] print(substring)
Output
Shitus
3. Extracting a substring to the end of the string:
string = "Shitus is a Free learning Blogs" substring = string[-5:] print(substring)
Output:
Blogs
4. Extracting every second character from a string:
string = "Shitus is a Free learning Blogs"
substring = string[::2]
print(substring)
Output
Siu saFe erigBos
5. Reversing a string:
string = "Shitus is a Free learning Blogs" reversed_str = string[::-1] print(reversed_str)
Output:
sgolB gninrael eerF a si sutihS
lower() and upper() case
These methods are used to convert the string to lowercase and uppercase, respectively. For example:
string = "Shitus is a Free learning Blogs" lower_case = string.lower() upper_case = string.upper() print(lower_case) print(upper_case)
Output:
shitus is a free learning blogs
SHITUS IS A FREE LEARNING BLOGS
strip()
This method removes any leading and trailing whitespace characters from the string. It is useful when dealing with user inputs. For example:
string = " Shitus is a Free learning Blogs " stripped_string = string.strip() print(stripped_string)
split() in Python strings
This method splits a string into a list of substrings based on a delimiter. By default, the delimiter is a space character. For example:
string = "Shitus is a Free learning Blogs" split_string = string.split() print(split_string)
Output
string = "Shitus is a Free learning Blogs"
split_string = string.split()
print(split_string)
join()
This method joins a list of strings into a single string, using the specified string as a delimiter. For example:
string_list = ['Shitus', 'is', 'a', 'Free', 'learning', 'Blogs'] joined_string = ' '.join(string_list) print(joined_string)
Output
Shitus is a Free learning Blogs
3 thoughts on “Python Strings: Everything You Need to Know”