0% found this document useful (0 votes)
4 views5 pages

Assignment 2

Uploaded by

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

Assignment 2

Uploaded by

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

8/23/22, 6:31 AM Assignment 2 - Jupyter Notebook

In [9]: #⦁ How would you check if each word in a string begins with a capital letter?
#it possible to convert first letter of each word in a string can be conv
#example
str1 = "data science and python"
str1.title()

Out[9]: 'Data Science And Python'

In [13]: #⦁ Check if a string contains a specific substring


str1 = "data SCIENCE and python"
if "SCIENCE" in str1:
print("String SCIENCE exist")
else :
print("String SCIENCE doesnot exist")

String SCIENCE exist

In [28]: #⦁ Find the index of the first occurrence of a substring in a string


str1 = "data SCIENCE and python data SCIENCE and python"
if "SCIENCE" in str1:
print("String SCIENCE exist")
else :
print("String SCIENCE doesnot exist")

str1.find("SCIENCE",0,50)

String SCIENCE exist

Out[28]: 5

In [32]: #Count the number of a specific character in a string


str1 = "data SCIENCE and python data SCIENCE and python data SCIENCE and python d
str1.count("a")

Out[32]: 12

In [35]: #Capitalize the first character of a string


str1 = "data SCIENCE and python"
str1.title()

Out[35]: 'Data Science And Python'

localhost:8888/notebooks/Assignment 2.ipynb 1/1


8/24/22, 3:17 PM Assignment 2 - Jupyter Notebook

In [ ]: #1. What is string? And how we can initialize it?


it is collection of characters enclosed within single or double or triple quotes.
string is an immutable type of datatype
we can initialize string by assinging some variable by quoting that inside single or double or triple
quotes and with "=" symbol

In [3]: #2. Write a Python program to calculate the length of a string


str = "Data science and python"
print(len(str))

23

In [5]: #2. Write a Python program to calculate the length of a string


str = "Data science and python"
len(str)

Out[5]: 23

In [ ]: #3. What are the Escape Characters in python?


\n - to print remaining string on next line
\t- to print remaining string on by leaving one tab space.
\r - to print remaining string by replacing the beginging string.(length of replaced string will
be equal to length string after \r)
\\ - to place backslash
\' - to place quote
\" - to place double quote

In [ ]: #4. Which are the different ways to perform string formatting? Explain with example.
there two ways to do string formatting
i) by using f string
ii) by using format function

In [13]: #example for f string--------Addition of two numbers


a = 50
b = 20
c=a+b
print(f"addition of {a} and {b} is :",c)

addition of 50 and 20 is : 70

In [31]: #example for format function--------Addition of two numbers


a = eval(input("enter the value of a:"))
b = eval(input("enter the value of b:"))
c = a + b
print("addition of {} and {} is :{}".format(a, b, c))

enter the value of a:20


enter the value of b:30
addition of 20 and 30 is :50

In [39]: #5. Write a program to find the length of the string "machine learning”.
str2 = "machine learning"
len(str2)

Out[39]: 16

In [40]: #6. Write a program to check if the word 'orange' is present in the "This is orange juice".
str3 = "this is orange juice"
"orange" in str3

Out[40]: True

In [57]: #7. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string.
str4 = "data science and python"
str5 = str4[0:2] + str4[-2:]
print(str5)

daon

localhost:8888/notebooks/Assignment 2.ipynb 1/4


8/24/22, 3:17 PM Assignment 2 - Jupyter Notebook

In [68]: #8. How would you confirm that 2 strings have the same identity
str1 = "data"
str2 = "data"
print(id(str1))
print(id(str2))

2258861134192
2258861134192

In [2]: #What is an f-string and how do you use it?


'''F-strings provide a way to embed expressions inside string literals,
In Python source code, an f-string is a literal string, prefixed with 'f'

Strings in Python are usually enclosed within double quotes ( "" ) or single quotes ( '' ).
To create f-strings, you only need to add an f or an F before the opening quotes of your string.
For example, "This" is a string whereas f"This" is an f-String.
'''

Out[2]: 'F-strings provide a way to embed expressions inside string literals,\nIn Python source code, an f-string is a li
teral string, prefixed with \'f\'\n\nStrings in Python are usually enclosed within double quotes ( "" ) or single
quotes ( \'\' ).\nTo create f-strings, you only need to add an f or an F before the opening quotes of your strin
g. \nFor example, "This" is a string whereas f"This" is an f-String.\n'

In [8]: #Search a specific part of a string for a substring


str1 = "aspire training institure pune"

if "pune" in str1:
print(" it is present in the string")
else:
print(" it is not present")

it is present in the string

In [10]: str1 = input("enter string")


str2 = input("enter substring to search")
str2 in str1

enter stringaspire training institute pune


enter substring to searchpune

Out[10]: True

In [12]: #Check if a string contains only numbers


string1 = ("sudhir123")
string1.isnumeric()

Out[12]: False

In [13]: #Check if a string contains only numbers


string1 = ("123")
string1.isnumeric()

Out[13]: True

In [15]: #Check if a string contains only numbers by using if_else statement


string1 = ("123")
if string1.isnumeric():
print("entered string contains only numerics")
else:
print("entered string contains alphabets and numerics")


entered string contains only numerics

In [16]: #Check if a string contains only numbers by using if_else statement


string1 = ("suhdir123")
if string1.isnumeric():
print("entered string contains only numerics")
else:
print("entered string contains alphabets and numerics")

entered string contains alphabets and numerics

localhost:8888/notebooks/Assignment 2.ipynb 2/4


8/24/22, 3:17 PM Assignment 2 - Jupyter Notebook

In [22]: #Split a string on a specific character


str1 = "aspire traininginstiturepune"
str1.split(" ")

Out[22]: ['aspire', 'traininginstiturepune']

In [23]: #Check if a string is composed of all lower case characters


str1 = "aspire training institure pune"
str1.islower()

Out[23]: True

In [30]: #Check if the first character in a string is lowercase


str1 = "Aspire training institure pune"
str1[0].islower()

Out[30]: False

In [27]: str="aspire training institure pune"


if str[0].islower():
print("The String starts with a lowercase Letter")
else:
print("The String starts with a uppercase Letter")

The String starts with a lowercase Letter

In [ ]: #Can an integer be added to a string in Python?


we can't . But we can change a number into a string if you use the str() function.

In [41]: #21. Reverse the string “hello world”


str1= "hello world"
str1[::-1]

Out[41]: 'dlrow olleh'

In [71]: #22. Join a list of strings into a single string, delimited by hyphens
str1 = "'aspire', 'pune'"
" ".join(['aspire', 'pune'])

Out[71]: 'aspire pune'

In [92]: #Uppercase first and last character of a string


str1= "hello world"
print(str1[0].upper() + str1[1:len(str1)-1] + str1[-1:].upper())

Hello worlD

In [97]: #Give an example of string slicing


str1 = "data science"
print(str1[5:10:])

scien

In [106]: #Convert an integer to a string


str5 = eval(input("enter the integer to convert it into string="))
print("string after converting from string",str5)

enter the integer to convert it into string=100


string after converting from string str5

In [107]: #Check if a string contains only characters of the alphabet


str1="data science amd python"
if str1.isalpha():
print("string contains only characters of alphabets")
else:
print("string does not contain only characters of alphabets")

string does not contain only characters of alphabets

localhost:8888/notebooks/Assignment 2.ipynb 3/4


8/24/22, 3:17 PM Assignment 2 - Jupyter Notebook

In [108]: #Check if a string contains only characters of the alphabet


str1="datascienceandpython"
if str1.isalpha():
print("string contains only characters of alphabets")
else:
print("string does not contain only characters of alphabets")

string contains only characters of alphabets

In [111]: #Replace all instances of a substring in a string


str1="twinkle twinkle little star"
str1.replace('twinkle twinkle little star' , 'data science and python')

Out[111]: 'data science and python'

In [117]: #Remove whitespace from the left, right or both sides of a string
str1= " twinkle twinkle little star "
str1.strip()

Out[117]: 'twinkle twinkle little star'

In [127]: #Check if a string begins with or ends with a specific character


str1="twinkle twinkle little start"
if str1.startswith("t",0) and str1.endswith("t",-1):
print("string starts and ends with specific char ")

else:
print("string only starts starts with specific char ")

string starts and ends with specific char

In [139]: #Concatenate Two strings


str1=input("enter the first string:")
str2=input("enter the second string")

print(str1+" "+str2)

enter the first string:data science


enter the second stringand python
data science and python

localhost:8888/notebooks/Assignment 2.ipynb 4/4

You might also like