In [1]:
my_string = "python"

In [2]:
print my_string.capitalize()


Python

In [ ]:
# string,intergers and float
# help

In [ ]:
# playing with strings

In [ ]:
# A string is sequence of characters

In [6]:
# p  y  t  h  o  n
# 0  1  2  3  4  5  # +ve indexing => left to right
# -6 -5 -4 -3 -2 -1 # -ve indexing => right to left

In [7]:
# indexing

In [9]:
print my_string[3]
print my_string[-1]
print my_string[5]


h
n
n

In [10]:
# string is immutable
# https://docs.python.org/2/library/gc.html

In [12]:
print my_string[0]
my_string[0]="P"


p
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-111ede840444> in <module>()
      1 print my_string[0]
----> 2 my_string[0]="P"

TypeError: 'str' object does not support item assignment

In [14]:
# slicing
# p  y  t  h  o  n
# 0  1  2  3  4  5  # +ve indexing => left to right
# -6 -5 -4 -3 -2 -1 # -ve indexing => right to left

In [18]:
# pyt -> 0 till 3 -> 3 not included.
print my_string[0:3]
print my_string[:3]
# hon
print my_string[3:6]
print my_string[3:]


pyt
pyt
hon
hon

In [29]:
print my_string[0:3]
print my_string[0:6]
# print my_string[begin:end:step]
print my_string[0:6:1]
print my_string[0:6:2]
print my_string[::3]
# for(i=10;i<1;i--)
print my_string[::-1]


pyt
python
python
pto
ph
nohtyp

In [19]:
# addition of strings
print my_string[:3] + my_string[3:]


python

In [20]:
# muliplication of string
print "*" * 20


********************

In [ ]:
# task
my_string = "python"
# output
# 'Tython'

In [22]:
print "T" + my_string[1:]
print my_string[2].upper() + my_string[1:]


Tython
Tython

In [23]:
# my_string.replace

In [30]:
# Playing with numbers - BODMAS

In [36]:
print 1 + 1
print 25 * 25
print 25/2
print 25/2.0
print 25 + 25/2
print (25 + 25)/2


2
625
12
12.5
37
25

In [ ]:
#printing

In [37]:
my_school = "De Paul"
another_school = "st. xaviers"
town = "township"
beach = "blue"
commute = "bus"

In [39]:
# print - old school
print "my school name is my_school"
print 'my school name is my_school'


my school name is my_school
my school name is my_school

In [44]:
print 'my school name is ', my_school , "we have another school adjancent to us ", another_school , 'we used to live in a small' , town


my school name is  De Paul we have another school adjancent to us  st. xaviers we used to live in a small township

In [45]:
# type casting - %s,%d,%f,%r
print "my school name is %s.I love my school %s" %(my_school,my_school)


my school name is De Paul.I love my school De Paul

In [53]:
# format
print "my school name is {}.I love my school {}.".format(my_school,another_school)
# indexing
print "my school name is {1}.I love my school {1}.".format(my_school,another_school)
# key based
print "my school name is {ms}.I love my school {ms}.".format(ms=my_school,ans=another_school)
# type casting
print "my school name is {!s}.I love my school {!s}.".format(my_school,another_school)


my school name is De Paul.I love my school st. xaviers.
my school name is st. xaviers.I love my school st. xaviers.
my school name is De Paul.I love my school De Paul.
my school name is De Paul.I love my school st. xaviers.

In [54]:
# input and raw_input

In [55]:
my_name = raw_input("please enter your name:")


please enter your name:kumar

In [56]:
print my_name,type(my_name)


kumar <type 'str'>

In [2]:
num = raw_input("please enter a number:")


please enter a number:22

In [3]:
print num,type(num)


22 <type 'str'>

In [4]:
num = int(raw_input("please enter a number:"))


please enter a number:22

In [5]:
print num,type(num)


22 <type 'int'>

In [7]:
num = int(raw_input("please enter a number:"))


please enter a number:a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-edb42c6cf795> in <module>()
----> 1 num = int(raw_input("please enter a number:"))

ValueError: invalid literal for int() with base 10: 'a'

In [8]:
# input -> 3.x and 2.x
# raw_input missing in 3.x

In [9]:
name = input("please enter your name:")


please enter your name:"vijay"

In [10]:
print name,type(name)


vijay <type 'str'>

In [11]:
num = input("please enter a number:")


please enter a number:2

In [12]:
print num,type(num)


2 <type 'int'>

In [ ]: