String in Python is identified as a contiguous set of characters represented in the quotation marks. One can use single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string. ie, "Dog", Hello World", "bad dude" etc. Lets take a look of the following example,
a = "The name of my dog "
b= "is "
c= "Tom"
age= "5"
print(a+b+c)
print("He is "+age+" years old.")
# Here "5" is a string. We can add two string easily. But if 5 is integer we cant
# add with string.
b= "is "
c= "Tom"
age= "5"
print(a+b+c)
print("He is "+age+" years old.")
# Here "5" is a string. We can add two string easily. But if 5 is integer we cant
# add with string.
and you will see the following output,
Lists:
Python has two data structures, list and tuples. The elements of lists or tuples can be numbers or strings, or both. Lists are defined by a pair of square bracket [ ] on either end of the individual element separated by commas. Individual elements of lists can be changed.
a = [ 4, 5, 4, 2, 8, 5, 8, 13 ]
b = [ 5.0, "Tom", "horse", 8, 21 ]
print( a[0] ) # We use zero based indexed
print( b[0] )
a[0]=b[0]+2 # we can add two integer, but cant add integer and string
b[1]=3.14159 # But string can be replaced by integer or floating number
print( a )
print( b ) # So, we have seen that list can be modified.
c=a+b # We can even add two lists together
print( c )
b = [ 5.0, "Tom", "horse", 8, 21 ]
print( a[0] ) # We use zero based indexed
print( b[0] )
a[0]=b[0]+2 # we can add two integer, but cant add integer and string
b[1]=3.14159 # But string can be replaced by integer or floating number
print( a )
print( b ) # So, we have seen that list can be modified.
c=a+b # We can even add two lists together
print( c )
and you will see the following output,
Tuples:
Tuples are lists that are immutable. It means, once declared, the elements cant be changed. A tuple is written as a sequence of numbers enclosed in round ( ) parentheses. Lets take a look of the following example.
a=( 4, 5, 8, 9, 23, "horse" )
b=( 7, 5, 21, 1.2 )
print( a[ 1 ] )
# Elements of tuple cant be changed
# however, different tuples can be created from other two
c=a+b;
print( c ) # new tuple c is created from a and b
b=( 7, 5, 21, 1.2 )
print( a[ 1 ] )
# Elements of tuple cant be changed
# however, different tuples can be created from other two
c=a+b;
print( c ) # new tuple c is created from a and b
and you will see the following output,
Python Dictionary:
Dictionary is similar like lists. However, dictionary elements can be changed. Each key in dictionary is separated from its value by a colon ( : ), the items are separated by commas, and the whole thing is enclosed in curly braces { }. ie, "Name": "Erdong Wang", "Age": 35 etc.
dict= { "Name": "Erdong Wang", "Age": 33, "Work": "Brookhaven Lab" }
print( "Name: ", dict["Name"] )
print( "Age: ", dict["Age"] )
print( "Workplace: ", dict["Work"] )
# We can change the elements of Dictionary
dict ["Age"] = 35
print( "Name: ", dict["Name"] )
print( "Age: ", dict["Age"] )
print( "Workplace: ", dict["Work"] )
print( "Name: ", dict["Name"] )
print( "Age: ", dict["Age"] )
print( "Workplace: ", dict["Work"] )
# We can change the elements of Dictionary
dict ["Age"] = 35
print( "Name: ", dict["Name"] )
print( "Age: ", dict["Age"] )
print( "Workplace: ", dict["Work"] )
and you will see the following output,