Friday, December 30, 2016

Methods in String: Practice


Strings in Python

  • '', "", and escape characters
  • Multiple strings with triple quotes
  • immutable
  • upper() and lower()
  • isuuper(), islower()
  • isalpha(), isalnum(), isspace(), isdecimal(), istitle()
  • startswith(), endswith()
  • join(), split()
  • ljust(), rjust(), center() => justifying texts
  • strip(), rstrip(), lstrip()
  • replace()
  • string formatting

'', "", and escape characters

In [5]:
print('hello world')
print("hello world")
print('hello\'world')
print('hello\nworld')
print('hello\tworld')
print('''hi
hi
hi''')
hello world
hello world
hello'world
hello
world
hello world
hi
hi
hi

upper() and lower()

In [8]:
s = "hello world"
print(s.upper())
print(s) #immutable
s = s.upper()
print("upper",s)
print(s.lower())
HELLO WORLD
hello world
upper HELLO WORLD
hello world

isupper() and islower()

In [7]:
s = "hello world"
print(s.islower())
print(s.isupper())
True
False

isalpha(), isalnum(), isspace(), isdecimal(), istitle()

In [15]:
print("hello".isalpha()) #True
print("hello123".isalpha()) #False
print("hi123".isalnum()) #True (both alpha and number)
print("Hello world"[5].isspace()) # True ("5th pos is space") 
print("12334".isdecimal()) #True
print("Hello My Name Is Arun".istitle()) #True (Every word starts in upper case)
True
False
True
True
True
True

startswith(), endswith()

In [16]:
h = "hello world"
print(h.startswith("hel"))
print(h.endswith("rld"))
True
True

join() , split()

join function is used to join a list of strings with a particular string.
split function is used to split a string based on a particular string value.
In [17]:
route = ["kanyakumari","chennai","bangalore","goa","mumbai","delhi","kashmir"]
print("-".join(route))
kanyakumari-chennai-bangalore-goa-mumbai-delhi-kashmir
In [18]:
route = "kanyakumari-chennai-bangalore-goa-mumbai-delhi-kashmir"
places = route.split("-")
print(places)
['kanyakumari', 'chennai', 'bangalore', 'goa', 'mumbai', 'delhi', 'kashmir']

ljust(), rjust(), center()

These functions are used to justify the string values
In [20]:
s = "hello"
print(s.ljust(10))
print(s.ljust(10,"*"))
print(s.rjust(10))
print(s.rjust(10,"*"))
print(s.center(10))
print(s.center(10,"*"))
hello     
hello*****
     hello
*****hello
  hello   
**hello***

strip(), rstrip(), lstrip()

In [29]:
s = "         hello           "
print(s)
print(len(s))
print(s.strip())
print(len(s.strip()))
print(s.rstrip())
print(len(s.rstrip()))
print(s.lstrip())
print(len(s.lstrip()))
         hello           
25
hello
5
         hello
14
hello           
16

replace()

In [30]:
s = "hi my name is arun"
s = s.replace("arun","prakash")
print(s)
hi my name is prakash

string formatting

In [38]:
name = "arun"
place = "chennai"
age = 24
print("Hi, my name is %s, and I'm from %s. I'm %d" %(name,place,age))
print("Hi, my name is {name}, and I'm from {place}. I'm {age} ".format(name = name,place = place, age = age))
Hi, my name is arun, and I'm from chennai. I'm 24
Hi, my name is arun, and I'm from chennai. I'm 24 

No comments :

Post a Comment