Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##------------------------------------------ Task 1 -----------------------------------------------##
- ## String object
- name = "Prince"
- print(len(name))
- print(name[0])
- print(name[-1])
- port = 2020
- print(str(port)[-1])
- ## Slice operation
- timestamp = "19-07-2023"
- day = timestamp[0:2]
- print(day)
- print(timestamp[3:5])
- year = timestamp[-4:]
- print(year)
- vehicle_no = "KL07-CA-3254"
- print(vehicle_no[8:])
- print(vehicle_no[-4:])
- ## For loop
- name = "prince"
- print("Program starts here")
- for item in name:
- print(item)
- print("Program ends here")
- lang = "malayalam"
- for char in lang:
- if char == "a":
- print(char)
- lang = "malayalam"
- x = 0
- for item in lang:
- if item == "a":
- x = x + 1
- print(x)
- ## Buil in functions
- ### lowerfn()
- name = "PrinCE"
- print("The name", name , "is converted to lower :", name.lower())
- ### upper
- name = "prince"
- print("The name", name , "is converted to upper :", name.upper())
- ### isdigit()
- s1 = "abc"
- s2 = "abc123"
- s3 = "123"
- print(s1, s1.isdigit())
- print(s2, s2.isdigit())
- print(s3,s3.isdigit())
- num = input("Enter the digit to check whether it's odd or even : ")
- if num.isdigit():
- if int(num) % 2 == 0:
- print("The number is even")
- else:
- print("The number is odd")
- else:
- print("Sorry invalid digit")
- ### isalpha
- s1 = "abc123"
- s2 = "123"
- s3 = "abc"
- print(s1, s1.isalpha())
- print(s2,s2.isalpha())
- print(s3,s3.isalpha())
- word = input("Enter the word to check : ")
- if word.isalpha():
- print("The word you entered is an alphabet")
- if word.isdigit():
- print("The input you entered is a digit")
- word = input("Enter a character : ")
- if len(word) == 1:
- if word.isalpha():
- print("Your input is an alphabet")
- if word.isdigit():
- print("Your input is a digit")
- else:
- print("Please enter one char only....try again, Thank you!")
- ### replace
- word = "malayalam"
- new = word.replace("a","A")
- print(word, "is now converted to upper A", new)
- new = word.replace("a"," ")
- print(new)
- new = word.replace("a","")
- print(new)
- ### count
- word = "Malayalam"
- new = word.count("a")
- new1 = word.count("l")
- new2 = word.count("m")
- print(new , new1, new2)
- word = "malayaLAm"
- x = 0
- y = 0
- for item in word:
- if item == "a":
- x = x + 1
- if item == "A":
- y = y + 1
- print("The number of a in the word", word , x)
- print("The number of A in the word", word , y)
- ### Starts and endswith
- conf = "/etc/httpf/conf/httpd.conf"
- result = conf.startswith("/")
- print(result)
- result = conf.startswith("/etc")
- print(result)
- result = conf.startswith("etc")
- print(result)
- result = conf.endswith(".conf")
- print(result)
- result = conf.endswith("httpd.conf")
- print(result)
- result = conf.endswith(".cfg")
- print(result)
- ### is upper and is lower
- s1 = "a"
- s2 = "AA"
- s3 = "ABCabc"
- s4 = "a12"
- s5 = "A"
- print(s1, s1.isupper())
- print(s1,s1.islower())
- print(s2,s2.isupper())
- print(s2, s2.islower())
- print(s3,s3.isupper())
- print(s3, s3.islower())
- print(s4,s4.isupper())
- print(s4, s4.islower())
- print(s5,s5.isupper())
- print(s5, s5.islower())
- ##------------------------------------------ Task 2 -----------------------------------------------##
- ## 1] Replace character with 23-12-2022 to 23/12/2022
- word = "23-12-2022"
- new = word.replace("-","/")
- print(new)
- Ans] 23/12/2022
- ### 2] Find the n.o of occurence of the char "a" or "A "
- word = "MAlayaLAm"
- x = 0
- y = 0
- for item in word:
- if item == "a":
- x = x + 1
- if item == "A":
- y = y + 1
- print("The number of a in the word", word,"is : ", x)
- print("The number of A in the word", word,"is : ", y)
- Ans]
- The number of a in the word MAlayaLAm is : 2
- The number of A in the word MAlayaLAm is : 2
- ### 3] Print the total n.o of digits.
- word = "a1b2c3D4E4F5"
- x = 0
- for item in word:
- if item.isdigit():
- x = x + 1
- print("The total n.o of digits in the word", word, "is: ", x)
- Ans] The total n.o of digits in the word a1b2c3D4E4F5 is: 6
- ### 4] Print the total n.o of small letters
- word = "a1b2c3D4E4F5"
- x = 0
- for item in word:
- if item.islower():
- x = x + 1
- print("The total n.o of small letters in the word", word,"is: ", x )
- Ans] The total n.o of small letters in the word a1b2c3D4E4F5 is: 3
- ### 5] Print the total n.o of Capital letters
- word = "a1b2c3D4E4F5"
- x = 0
- for item in word:
- if item.isupper():
- x = x + 1
- print("The total n.o of Capital letters in the word", word,"is: ", x )
- Ans] The total n.o of Capital letters in the word a1b2c3D4E4F5 is: 3
- ### 6] Find the number of spaces
- sentence = "This post is to illustrate how to access the docker container network namespace"
- x = 0
- for item in sentence:
- if item == " ":
- x = x + 1
- print("the number of spaces is: ",x)
- Ans] the number of spaces is: 12
- ### 7] Fix the error
- #### Corrected code
- number = input("enter a number ")
- if number.isdigit():
- if int(number) % 2 == 0:
- print("even")
- else:
- print("odd")
- else:
- print("Sorry you've entered a wrong input")
Advertisement
Add Comment
Please, Sign In to add comment