Python String and numbers

When working with data, we need ways to store it in variables so we can manipulate it.

three of the most common data types used in programming: numbers, strings and booleans. We assigned those data types to variables one-by-one, like so:

With working code below find examples and learn Python easily..



x = 3          # numbers
a = "gorillas" # strings
t = True       # booleans

PS :  To show OutPut we have used  >>  sign

Numbers

width = 20
height = 5 * 9
print width*height;
print(width*height);
>> 900
>> 900

Strings


But what if we need something more complicated, like a shopping list? Assigning a variable for every item in the list would makes things very complicated:

They can be enclosed in single quotes ('...') or double quotes ("...") with the same result

item_1 = "milk"
item_2 = "cheese"
item_3 = "bread"

print 'doesn\'t'  # use \' to escape the single quote...
print "doesn't"  # ...or use double quotes instead

>> doesn't


s = 'First line.\nSecond line.'
# \n means newline
print s
>> First line.
Second line.

If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:

print r'C:\some\name'  # note the r before the quote
C:\some\name

Strings can be concatenated (glued together) with the + operator, and repeated with *:

print 3 * 'un' + 'ium'
>> unununium

If you want to concatenate variables or a variable and a literal, use +:

prefix = 'Dhaval'
prefix = prefix + 'Devil'
print prefix
>> DhavalDevil

See Working code at http://ideone.com/cF38M7