Share:

Working With String In Python - Amit Padhiyar (Saatody)

Python is very popular programming language. It is also very easy for developer. Python gives you many ways to make your program easy in every new release. In this post, I will discuss about only string (no discussion about string functions). I trying to make this post for developers who want to know "How to define string in Python?", "How Python different from other languages?", "How Python provides string handling syntax?".

So first, What is string in every programming language? The short answer is... The string is sequence of characters that use for store string or text values.

Syntax:

"Define String In Double Quotes"
'Define String In Single Quotes'

Python does not give an error when we use string in single quotes

Python does not give an error when we use string in single quotes when other programming languages gives error. This is a reason that the python is very easy for use. What is reason behind this? Because, The single quotes use for define characters in other programming language. But in Python, There is no way to define characters directly or using single quotes. for example you make a character in single quotes that is also consider as string in Python.

# Program
c1 = "C"
c2 = 'C'

print(type(c1))
print(type(c2))

# Output
<class 'str'>
<class 'str'>

Define string using single and double quotes in python

# Program
s1 = "Hello Saatody, Using Python!"
s2 = 'Hello Saatody, Using Python!'

print(s1)
print(s2)

# Output
Hello Saatody, Using Python!
Hello Saatody, Using Python!

Define multi lines string using single and double quotes in python

# Program
s1 = """Hello Saatody, 
Using Python!"""

s2 = '''Hello Saatody, 
Using Python!'''

print(s1)
print('') # Just for leave space
print(s2)


# Output
Hello Saatody,
Using Python!

Hello Saatody,
Using Python!

Deal with escape sequence characters

# Program
s1 = "Amit' Website"
s2 = 'Amit' Website'

print(s1)
print(s2)

# Output
  File "my.py", line 3
    s2 = 'Amit' Website'
                      ^
SyntaxError: invalid syntax

Look at the program, There is 'Amit' Website' where single quotes start before A and end after t. Here the Website' is look like a different string with error. So we use \' instead of ' (single quote). Check out below right program.

# Program
s1 = "Amit' Website"
s2 = 'Amit\' Website'

print(s1)
print(s2)

# Output
Amit' Website
Amit' Website

Ignore escape sequence

# Program
s1 = r"The folder is C:\New Folder\Python"
print(s1)

# Output
The folder is C:\New Folder\Python

Print string for multiple time

# Program
s1 = "A" * 5
s2 = 3 * "B"
s3 = 3 * "Hello\n"
print(s1)
print(s2)
print(s3)

# Output
AAAAA
BBB
Hello
Hello
Hello

Break long strings

# Program
s1 = ("Hello Developers! "
     "I Am Amit From Saatody. "
  "I Am Founder Of Saatody. "
  "I ❤️ Python! ")

print(s1)

# Output
Hello Developers! I Am Amit From Saatody. I Am Founder Of Saatody. I ❤️ Python!

Concatenate, join or merge two strings

# Program
s1 = "Py" "thon"
print(s1)

# Output
Python

# Program
s1 = "Py"
s2 = s1 "thon"
print(s2)

# Output
  File "my.py", line 3
    s2 = s1 "thon"
                 ^
SyntaxError: invalid syntax

# Program
s1 = "Py"
s2 = "thon"
s3 = s1 + "thon"
s4 = s1 + s2
print(s3)
print(s4)

# Output
Python
Python

Note: In above 1st program, two string literals concatenate like that. But variables can't concatenate with string literal like first method. It will give an error like 2ed program. Look at the 3ed program, How to concatenate two string . variable to string literal and variable to variable.

String strucute in Python

 +---+---+---+---+---+
 | A | B | C | D | E |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

Get particular character from string

# Program
s1 = "ABCDE"
print(s1[0])
print(s1[1])
print(s1[2])
print(s1[3])
print(s1[4])

# Output
A
B
C
D
E

Get particular character from the reverse of the string

# Program
s1 = "ABCDE"
print(s1[-1])
print(s1[-2])
print(s1[-3])
print(s1[-4])
print(s1[-5])

# Output
E
D
C
B
A

Get particular part from string

 # Program
s1 = "ABCDE"

print(s1[0:5]) # ABCDE
print(s1[0:4]) # ABCD
print(s1[2:4]) # CD

print(s1[0:]) # ABCDE
print(s1[1:]) # BCDE
print(s1[3:]) # DE

print(s1[:5]) # ABCDE
print(s1[:4]) # ABCD
print(s1[:3]) # ABC

print(s1[-4:-2]) # BC
print(s1[1:-2])  # BC
print(s1[:-2])   # ABC
print(s1[-4:])   # BCDE

print(s1[0::2])  # ACE
print(s1[:5:2])  # ACE
print(s1[0:5:2]) # ACE

print(s1[-5:5:2])  # ACE
print(s1[-5:-1:2]) # AC
print(s1[-4:3:2])  # B

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

Create Drag And Drop Operation Between DevExpress GridControl And Custom WPF UI | Saatody | Amit Padhiyar