In this tutorial, we will discuss one of the most popular data type in Python, i.e. string. Python string is the collection of the characters surrounded by single quotes, double quotes or triple quotes. The computer doesn't understand the characters internally, it stores manipulated character as the combination of 0's and1's.
Each character is encoded in the ASCII or Unicode character. So Python strings are also called the collection of Unicode characters.
Syntax:
str = "Hi Python!" |
---|
We can also check the type of the string variable using a Python script.
|
---|
Output:
<class 'str'> |
---|
|
---|
Learn Python Learn Python Used to write multiline string |
---|
String indexing and splitting
STRING: P
Y T H O N
str[2]='T'
s = "Python" print(s[0]);print(s[1]);print(s[2]);print(s[3]);print(s[4]);print(s[5]) |
---|
P y t h o n |
---|
As shown in above example, the slice operator[] is used to access the individual character of a string. However, we can use (:) colon operator to access the substring from the given string.
STRING: P Y T H O N
str[2]='T'
s = "Python" print(s[:]);print(s[1:3]);print(s[0:2]);print(s[0:]);print(s[4:5]);print(s[:5]) |
---|
Python yt Py Python 0 Pytho |
---|
STRING: P Y T H O N
s = "Python" print(s[-1]);print(s[-2]);print(s[-3]);print(s[0:-2]);print(s[-4:-1]);print(s[-5:-3]) |
---|
n o h Pyth tho yt |
---|
s = "HELLO" print(s) s = "hello" print(s) |
---|
HELLO hello |
---|
s = "HELLO" del s print(s) |
---|
NameError: name 's' is not defined |
---|
Operators |
Description |
+ |
It is known as
concatenation operator, used to join the strings. |
* |
It is known
as the repetition operator and used to concatenate the multiple copies of the
same string. |
[] |
It is known as slice
operator, used to access the sub-strings of a particular string. |
[:] |
It is known
as range slice-operator, used to access the characters from the specified
range. |
In |
It is known as
membership operator. It returns if a particular sub-string is present in the
specified string. |
not in |
It is also
known as membership operator and is just reverse to in operator. It returns ‘true’
if a particular sub-string is not present in the specified string. |
r/R |
It is used to specify
the raw string. Raw strings are used in the cases where we need to print the
actual meaning of escape characters such as “C://python”. To define any
string to a raw string, the character r or R is followed by the string. |
% |
It is used to
perform string formatting. It makes use of the format specifiers used in C
programming like %d or %f to map their values in Python. We will discuss how
formatting is done in Python. |
s = "learn" s1 = " python" print(s+s1) #concatenate the strings s and s1 print(s1*3) # repeats the string s1 3 times print(s[4]) #print 4th character of string s print(s[1:5]) #print the characters of string s after 1st and till 5th character print('o' in s) # if 'o' is present in s, will print true otherwise false. print('o' in s1) # if 'o' is present in s1, will print true otherwise false. print('e' not in s) # if 'e' is not present in s, will print true otherwise false. print(r'C://python') #print ://python as it is. print("the string str: %s"%(s)) #print the sring str: learn. |
---|
learn python python python python n earn false true false C://python the string str: learn |
---|
s = "They said, "What's your occupation?"" print(s) |
---|
SyntaxError: invalid syntax |
---|
#using triple quotes print('''They said, "What's your occupation?"''') #escaping single quotes print('They said, "What\'s your occupation?"') #escaping double quotes print("They said, \"What's your occupation?\"") |
---|
They said, "What's your occupation?" They said, "What's your occupation?" They said, "What's your occupation?" |
---|
Sr. |
Escape Sequence |
Description |
Example |
Output |
1. |
\newline |
It ignores the new
line |
print(“Python1 \ Python2 \ Python3”) |
Python1 Python2
Python3 |
2. |
\\ |
Backslash |
print(“\\”) |
\ |
3. |
\’ |
Single Quotes |
print('\'') |
‘ |
4. |
\\” |
Double Quotes |
print("\"")
|
“ |
5. |
\a |
ASCII Bell |
print(“\a”) |
|
6. |
\b |
ASCII Backspace |
print(“Hello \b
world”) |
Hello world |
7. |
\f |
ASCII Formfeed |
print(“Hello \f
world!”) |
Hello world! |
8. |
\n |
ASCII Linefeed |
print(“Hello \n
world!”) |
Hello world! |
9. |
\r |
ASCII Carriege
Return(CR) |
print(“Hello \r
world!”) |
world! |
10. |
\t |
ASCII Horizontal Tab |
print(“Hello \t
world!”) |
Hello world! |
11. |
\v |
ASCII Vertical Tab |
print(“Hello \v
world!”) |
Hello world! |
12. |
\ooo |
Character with octal
value |
print("\110\145\154\154\157")
|
Hello |
13. |
\xHH |
Character with hex
value |
print("\x48\x65\x6c\x6c\x6f") |
Hello |