0% found this document useful (0 votes)
17 views4 pages

Python Work 28-03-24

Uploaded by

Aniket Parashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Python Work 28-03-24

Uploaded by

Aniket Parashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON

import sys
'''
str1 = "HELLO PYTHON"

print(str1)

mystr = 'Hello World' # Define string using single quotes


print(mystr)

mystr = "Hello World" # Define string using double quotes


print(mystr)

mystr = Hello
World # Define string using triple quotes
print(mystr)

mystr = """Hello
World """ # Define string using triple quotes
print(mystr)

mystr = ('Happy '


'Monday '
'Everyone') # printing happy monday everyone
print(mystr)

mystr2 = 'Woohoo ' # printing Woohoo 5 times


mystr2 = mystr2*5
print(mystr2)

print(len(mystr2)) ##printing length of mystr2

print(str1[0]) # First character in string "str1"

print(str1[len(str1)-1]) # Last character in string using len function

print(str1[6]) #Fetch 7th element of the string

print(str1[5])

print(str1[0:5]) ##printing first 5 letters

print(str1[6:12]) ##printing characters between 6 - 12 index

print(str1[-4:]) ##printing characters THON

print(str1[-6:]) ## Retreive last six characters of the string

print(str1[:4]) # Retreive first four characters of the string


PYTHON

print(str1[:6]) ##retriving 1st 6 character of string

print(str1)

str1[0:5] = 'HOLAA'
print(str1)

del str1 #delete a string


print(str1)

# String concatenation
s1 = "Hello"
s2 = "Asif"
s3 = s1 + s2
print(s3)
'''

''' ITERATING THROUGH A STRING '''


'''
mystr1 = "Hello Everyone" # printing hello everyone in a line
print(mystr1)

for i in mystr1: #printing hello everyone in line by line


print(i)

for i in enumerate(mystr1): #printing helloeveryone line by line


print(i)

for i in enumerate(mystr1):
print(i)

print(list(enumerate(mystr1))) # Enumerate method adds a counter to an


iterable and ret

mystr1 = "Hello Everyone "


print ('Hello' in mystr1)
print ('Everyone' in mystr1)
print ('Hi' in mystr1) ##checking substring 'hi' present or not

str5 = "Natural language processing with Python and R and Java"


L = str5.partition("and")
print(L)

' STRING FUNCTION'

mystr2 = " Hello Everyone "


print(mystr2)
PYTHON

mystr2 = " Hello Everyone "


mystr2.strip() # Removes white space from begining & end
print()

mystr2 = " Hello Everyone "


print(mystr2.rstrip()) # Removes all whitespaces at the end of the string
print(mystr2.lstrip())

mystr2 = "*********Hello Everyone***********All the Best**********"


print(mystr2) ##printing Hello EveryoneAll the Best

mystr2.strip('*') # Removes all '*' characters from begining & end of the
string
print(mystr2)

mystr2.rstrip('*') # Removes all '*' characters at the end of the string


print(mystr2)

mystr2.lstrip('*') # Removes all '*' characters at the begining of the string


print()

mystr2 = " Hello Everyone "


print(mystr2)

mystr2.lower() # Return whole string in lowercase


print(mystr2.lower())

mystr2.upper() # Return whole string in uppercase


print(mystr2.upper())

mystr2.replace("He" , "Ho") #Replace substring "He" with "Ho"


print(mystr2)

mystr2.replace(" " , "")


print(mystr2)

mystr5 = "one two Three one two two three"


print(mystr5)
print(mystr5.count("one"))# Number of times substring "one" occurred in
string.

mystr5.count("two") # Number of times substring "two" occurred in string.


print(mystr5.count("two"))

mystr5.startswith("one") # Return boolean value True if string starts with


"one"
print(mystr5.startswith("one"))
PYTHON

mystr5.endswith("three") # Return boolean value True if string ends with


"three"
print(mystr5.startswith("one"))
'''

mystr4 ="one two three four one two two three five five six seven six seven
one"
print(mystr4)

mylist = mystr4.split() # Split String into substrings


print(mylist)

You might also like