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.
str1
str2
So, we will get the following output Hello World.
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.
HA
# string str = "HA" # replicate result = str * 3 # output print(result)
The above code will give us a new string HAHAHA.
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.
True
False
In the following Python program we are checking if the search string lo is present in the given string Hello World.
lo
# 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.
Not found
We use the str[i] to get the character at i index in the given string str.
str[i]
i
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.
str[start:end]
We start from the start index and extract substring till the end index without including it.
start
end
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].
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.
step
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].
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].
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.
dlroW olleH
Escape sequence represents non-printable characters. They start with backslash.
Following are some of the escape sequence.
\a
\b
\e
\f
\n
\r
\s
\t