Showing posts with label make list with python string. Show all posts
Showing posts with label make list with python string. Show all posts

Monday, July 6, 2015

Python String

Python string is a delicately build data type in python. You have to write string like this  -
a="This is a string"
you can print in upper case by
a.upper() #"THIS IS A STRING" is the output
and in lowercase by
a.lower() #"this is a string" is the output
a.swapcase() #"tHIS IS A STRING" - swaps the case of the string
a.split()
['This','is','a','string']
#Creates the list by dividing the string by spaces
or
you can split the strin with other characters too. e.g ,,>,any character

splitting a string by example :

open your python idle or notepad and write the following  -

a=raw_input("Enter a your full name")
a=str(a)
a=a.split()
print "Welcome %s"%a[0]
print "Your surname is %s"%a[1]

save the program with a .py extension and run it.You will see you not only split the entered name to first name and surname ut printed them in a suitable manner by storing them in a list.

For more information and knowledge base go to www.pythonbeginner.in and watch the video below.

You can also slice string by the following option -
Suppose you have a string
>> a="Hello, this is me"
then 
>>a[0] will print 'H'
>> a[1] will print 'e'
>>a[0:3] will print 'Hel'
>>a[1:4] will print 'ell'
>>a[-1] will print d
>>a[9:-1] will print 'l'

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...