Python
In this tutorial we will learn to operate on strings in Python.
We can concat two string values using the +
operator.
In the following example we are concatenating the two strings to get a third string.
# strings
str1 = "Hello"
str2 = "World"
# concat
result = str1 + " " + str2
# output
print(result);
The above program will concat (join) the first string str1
followed by a space and then the second string str2
.
So, we will get the following output Hello World
.
We can replicate a given string N times using the *
operator.
In the following example we are replicating the string HA
3 times.
# string
str = "HA"
# replicate
result = str * 3
# output
print(result)
The above code will give us a new string HAHAHA
.
in
We can use the in
operator to check whether a search string is present in a given string. We get True
if search string is found, False
otherwise.
In the following Python program we are checking if the search string lo
is present in the given string Hello World
.
# strings
needle = "lo"
haystack = "Hello World"
# check
if needle in haystack:
print(needle, "is present in the string", haystack)
else:
print("Not found")
We will get the following output for the above program.
lo is present in the string Hello World
not in
We can use the not in
operator to check if a search string is not present in a given string. We get True
if search string is not found, False
otherwise.
In the following Python program we are checking if the search string HA
is present in the given string Hello World
.
# strings
needle = "HA"
haystack = "Hello World"
# check
if needle in haystack:
print(needle, "is present in the string", haystack)
else:
print("Not found")
We will get Not found
as the output of the above code.
We use the str[i]
to get the character at i
index in the given string str
.
Index starts from 0 so, first character is at index 0, second is at 1 and so on.
In the following example we are extracting the second character (index 1) from the string "Jane Doe".
# string
str = "Jane Doe"
# character
ch = str[1]
# output
print(ch) # a
We use str[start:end]
to get a substring from a given string.
We start from the start
index and extract substring till the end
index without including it.
In the following example we are extracting the substring lo
from the string Hello World
.
# string
str = "Hello World"
# substring
substr = str[3:5]
# output
print(substr) # lo
We can skip characters of a string using str[start:end:step]
.
Where, start
is the starting index. end
represents the last index (not included) till the string is extracted and step
is the number of steps to take.
In the following example we are skipping 1 character for the given string "Hello World".
The string "Hello World" has 11 characters and we want to skip odd indexed characters so, step will be 2.
Since we are considering the entire string so, we can ignore end
. We will use str[start::step]
.
So, we will consider the following characters: 0->2->4->6->8->10.
string: Hello World
skipping: x x x x x # characters marked with x
final str: HloWrd
# string
str = "Hello World"
# skip
new_str = str[0::2]
The easiest way to reverse a string in Python is by writing str[::-1]
.
In the following Python program we are reversing the string "Hello World".
# string
str = "Hello World"
# reverse
result = str[::-1]
# output
print(result)
The above code will give us the following output dlroW olleH
.
Escape sequence represents non-printable characters. They start with backslash.
Following are some of the escape sequence.
Description | Escape sequence notation |
---|---|
Alert or Bell | \a |
Backspace | \b |
Escape | \e |
Form feed | \f |
New line | \n |
Carriage return | \r |
Space | \s |
Tab | \t |
ADVERTISEMENT