Escape sequence in python

 Sequence of character after backslash ‘\’ are called escape sequence character.

Escape sequence character comprises of more than on character but represent one character when used within the string.

Some of the commonly used escape sequence characters are :-

‘\\’Backslash character
‘\”single quote character
‘\n’Newline character
‘\t’Tab character

Example :-

# for backslash 
print("This is a\\ string")
 
# for single quote in string
print("This is \'a\' string")
 
#for tab 
print("This is a\t string")
 
# for line break
print("This is a\n string")
Python

Output :-

This is a\ string   
This is 'a' string                                                                                                                            
This is a        string                                                                                                                       
This is a                                                                                                                                     
 string 
Markup

These are the most commonly used escape sequence characters. I hope you understand this tutorial. If you have query or suggestion please do a comment.

Thank You.

String In Python

 A string is a sequence if characters. It can be declared in python by using double quotes. String are immutable i.e, They can not be changed.

We can primary write string in three ways :-

  1. Single quote string :- ‘Codeupon’
  2. Double quote string :- “Codeupon”
  3. Triple quote string :- ”’Codeupon”’

String slicing

A string in python can be sliced for getting a part of the string.

Syntax :-

variable_name [start index : end index : gap/skip index]

Example #1 :- Slicing the string

str = "codeupon"
print(str[0:5])
Python

Output :-

codeu
Markup

Example #2 :- Slicing string with skip values

str = "codeupon"
 
print(str[0:5:2])
Python

Output :-

cdu
Python

Example #3 :- Reversing a string

str = "codeupon"
print(str[::-1])
Python

Output :-

nopuedoc 
Python

String Functions

Some of the mostly used functions to perform operation on or manipulate strings are :-

  1. len () Function :- This function is used to find the length of the string.

Example :-

word = "codeupon"
print(len(word))
Python

Output :-

7
Markup

2. string.endwith (“bye”) :- This function tells whether the string ends with the string “bye” or not. It return True or False on the basis of string present or not.

Example :-

str = "Good bye"
print(str.end("bye"))
Python

Output :-

True
Markup

3. string.find(substring) :- This function is used to find the sub string in the given string. Its return the position of the sub string from the string.

Example :-

str = "Goodbye"
print(str.find("bye"))
Python

Output :-

4
Markup

There are soo many string function are in python if you want to explore more then go through this link https://www.w3schools.com/python/python_strings.asp

I hope you find this tutorial helpful. If you have query or suggestion please do a comment.

Thank You.

( == ) and Is Operator in Python

 ( == ) Double equal to operator

The double equal operator compares the values of both the operands and checks for value equality.

Example #1:-

a = 5
b = 5
 if a == b:
    print(True)
else:
    print(False)
Python

Output :-

True 
Markup

is operator

is operator checks whether both operands refer to the same object or not means both refers to same memory location or not.

It check only both variables refer to same memory location.\

Example #2 :-

a = 5
b = 3

if a is b:
    print(True)
else:
    print(False)
Markup

Output :-

False
Markup

It return False because the value of the variables are same but the memory location of both variables are different.

I hope you understand the concept of ( == ) and is operator in python. If you have any query or suggestion please do a comment.

Thank you.

Any and All Operator In Python

Any operator

This operator return True if any of the items in the list is True. It return False if empty or all the items are False.

Example #1:-

print(any([False,False, False]))
Python

Output :-

False
Markup

Example #2:-

print(any([False, True, False]))
Python

Output :-

True
Markup

Example #3 :- Program using any operator

list1 = []
list2 = []
 
for i in range(10):
    list1.append(4*i)
     
for i in range(10):
    c = list1[i] % 5 == 0
    list2.append(c)
 
print(list2)
print(any(list2))
Python

Output :-

[True, False, False, False, False, True, False, False, False, False]  
                                                                        
True
Markup

 

Explanation :- Its return True because in the list its find one item is True

All operator

This all operator return True if all the items of the list are True, if any one of them is False then it will return False.

Example #4:-

print(all([True, True, True]))
 
print(all([True, False, True]))
Python

Output :-

True
False
Markup

 Example #5 :-

list1 = []
list2 = []
 
for i in range(21):
    list1.append(4*i-3)
     
for i in range(20):
    c = list1[i] % 2 == 1
    list2.append(c)
 
print(list2)
 
print(any(list2))
Python

 Output :-

[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
                      
True
Markup

 Explanation :- Its return True because all the items in the list are True.

I hope You understand the concept of the Any and All operator in python. If you have any query or suggestion please do a comment.

Thank You.

Featured Post

List in Python

  List are just like dynamic array, declared in other languages( like vector in c++ and array list in java ). A single list may contain data...