Showing posts with label Python: Strings. Show all posts
Showing posts with label Python: Strings. Show all posts

Python: Strings

 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.

s = "Hi Python!"
print(type(s))

Output:
<class 'str'>

In Python strings are treated as the sequence of characters, that means Python doesn't support the character data type, instead a single character written as "A" is treated as the string of length 1.

Creating String in Python
We can create a string by enclosing the characters in single quotes or double quotes. We can also use triple quotes to represent multiline strings called docstrings.

#Single quotes string
s1 = 'Learn Python'
#Double quotes string
s2 = "Learn Python"
#Triple quotes string
s3 = '''Used to 
write
multiline string'''

print(s1);print(s2);print(s3)

Output:
Learn Python
Learn Python
Used to 
write
multiline string

String indexing and splitting
Like other programming languages, the indexing of the Python string starts from'0'.

For example:

 STRING:            P        Y      T       H      O     N

INDEXING:                    0              1           2             3           4          5       

In above example string PYTHON is indexed as above. 
Here, letters are indexed as:
str[0]='P'
str[1]='Y'
str[2]='T'
str[3]='H'
str[4]='O'
str[5]='N'

Check the above example in Python coding:
s = "Python"
print(s[0]);print(s[1]);print(s[2]);print(s[3]);print(s[4]);print(s[5])

Output:
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.

For example:

 STRING:            P        Y      T       H      O     N

INDEXING:                     0              1            2            3           4          5       

str[0]='P'
str[1]='Y'
str[2]='T'
str[3]='H'
str[4]='O'
str[5]='N'

str[:]='PYTHON'
str[0:]='PYTHON'
str[1:4]='YTH'
str[0:2]='PY'

Let's check the code
s = "Python"
print(s[:]);print(s[1:3]);print(s[0:2]);print(s[0:]);print(s[4:5]);print(s[:5])

Output:
Python
yt
Py 
Python
0
Pytho

We can do the negative slicing in the string; it starts from the rightmost character, which is indicated as -1, the second rightmost character is indicated as -2, third rightmost character as -3 and so on.

Consider the following image:

 STRING:            P       Y      T       H      O     N

INDEXING:                   -6        -5        -4            -3          -2         -1

str[-1]='N'
str[-2]='O'
str[-3]='H'
str[-4]='T'
str[-5]='Y'
str[-6]='P'

str[-3:-1]='HO'
str[-4:-1]='THO'
str[-5:-3]='YT'
str[0:-2]='PYTH'

Let's check the code
s = "Python"
print(s[-1]);print(s[-2]);print(s[-3]);print(s[0:-2]);print(s[-4:-1]);print(s[-5:-3])

Output:
n
o

Pyth
tho
yt


Reassigning Strings: Updating the content of the string is as easy as assigning it to a new string. But a string can only be replaced with new string since its content can't be replaced partially. Strings are immutable in Python.

For example

s = "HELLO"
print(s)
s = "hello"
print(s)

Output:
HELLO
hello

Deleting the string: As we know that the strings are immutable. We cannot delete or remove the characters from the string. But we can delete the entire string by del keyword.

For example

s = "HELLO"
del s
print(s)

Output:
NameError: name 's' is not defined

String Operators:

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.


For example

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.

Output:
learn python
python python python
n
earn
false
true
false
C://python
the string str: learn

Python String Formatting:
Escape Sequence
Let's suppose we need to write the text as -They said, "What's your occupation?" - the given statement can be written in single quote or double quote but it will raise the SyntaxError as it contains both single and double quotes.

For example

s = "They said, "What's your occupation?""
print(s)

Output:
SyntaxError: invalid syntax

However we can use triple codes to accomplish this problem but Python provides the escape sequence.
The backslash (\) symbol denotes the escape sequence. The backslash can be followed by a special character and it interpreted differently. The single quotes inside the string must be escaped. We can use the same as in double quotes also.

For example

#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?\"")

Output:
They said, "What's your occupation?"

They said, "What's your occupation?"

They said, "What's your occupation?"

The list of an escape sequence is given below:

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

Python: Functions