Repository navigation
Expand file tree
/
Copy path097-String-Address-and-Value-Comparison.py
More file actions
108 lines (86 loc) · 3.42 KB
/
Copy path097-String-Address-and-Value-Comparison.py
File metadata and controls
108 lines (86 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
===============================================================================
File Name : 97-String-Address-and-Value-Comparison.py
Description : String Address and Value Comparison in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
Strings can be compared in two different ways:
1. Value Comparison using (==)
2. Address Comparison using (is)
== Operator:
Checks whether the values (contents) of two strings are equal.
is Operator:
Checks whether both variables refer to the same object in memory.
Syntax:
string1 == string2
string1 is string2
Example:
name1 = "Python"
name2 = "Python"
print(name1 == name2)
print(name1 is name2)
"""
# -----------------------------------------------------------------------------
# Creating the first string.
# -----------------------------------------------------------------------------
string1 = "Python"
# -----------------------------------------------------------------------------
# Creating the second string with the same value.
# -----------------------------------------------------------------------------
string2 = "Python"
# -----------------------------------------------------------------------------
# Creating the third string with a different value.
# -----------------------------------------------------------------------------
string3 = "Java"
# -----------------------------------------------------------------------------
# Displaying the string values.
# -----------------------------------------------------------------------------
print("String 1 :", string1)
# Output:
# String 1 : Python
print("String 2 :", string2)
# Output:
# String 2 : Python
print("String 3 :", string3)
# Output:
# String 3 : Java
print()
# -----------------------------------------------------------------------------
# Comparing the values of string1 and string2.
# -----------------------------------------------------------------------------
print("string1 == string2 :", string1 == string2)
# Output:
# string1 == string2 : True
# -----------------------------------------------------------------------------
# Comparing the memory addresses of string1 and string2.
# -----------------------------------------------------------------------------
print("string1 is string2 :", string1 is string2)
# Output:
# string1 is string2 : True
print()
# -----------------------------------------------------------------------------
# Comparing the values of string1 and string3.
# -----------------------------------------------------------------------------
print("string1 == string3 :", string1 == string3)
# Output:
# string1 == string3 : False
# -----------------------------------------------------------------------------
# Comparing the memory addresses of string1 and string3.
# -----------------------------------------------------------------------------
print("string1 is string3 :", string1 is string3)
# Output:
# string1 is string3 : False
print()
# -----------------------------------------------------------------------------
# Displaying the memory address of each string.
# -----------------------------------------------------------------------------
print("Address of string1 :", id(string1))
# Output:
# Address of string1 : (Memory Address)
print("Address of string2 :", id(string2))
# Output:
# Address of string2 : (Memory Address)
print("Address of string3 :", id(string3))
# Output:
# Address of string3 : (Memory Address)