DS060623 - Practice Questions for This Weekend

1. Given a string s consisting of words and spaces, return the length of the last word in the string.

Input: s = " fly me to the moon " *
Output: 4
Explanation: The last word is “moon” with length 4.

2.Write a python program to print the cube of all numbers from 1 to a given number.

3.Write a Python program to count the number of characters (character frequency) in a string.

Sample String : googlecom
Expected Result : {‘g’: 2, ‘o’: 3, ‘l’: 1, ‘e’: 1, ‘c’: 1, ‘m’: 1}

4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.

Sample String : ‘hello’
Expected Result : ‘helo’
Sample String : ‘w3’
Expected Result : ‘w3w3’
Sample String : ’ w’
Expected Result : Empty String

5.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

Sample String : ‘abc’, ‘xyz’
Expected Result : ‘xyc abz’

2 Likes

1. Given a string s consisting of words and spaces, return the length of the last word in the string.

string = " fly me to the moon "

lst = string.split()

print(f’Length of the last word is {len(lst[-1])}')

2.Write a python program to print the cube of all numbers from 1 to a given number.

num = int(input(“Enter a number :”))
for i in range(1,num+1):
print(i**3,end=" ")

3.Write a Python program to count the number of characters (character frequency) in a string.

string=“googlecom”
param = {}
for i in string:
param[i]=string.count(i)
print(param)

4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.

string = input("Enter a string : ")
a=string[0:2]
b=string[-2:]
if len(string)>1:
print(a+b)
else:
print(“Empty String”)

5.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

string1 = “abc”

first_char1=string1[0:2]

string2 = “xyz”

first_char2=string2[0:2]

y=string1.replace(first_char1,first_char2)

z=string2.replace(first_char2,first_char1)

x = y+" "+z

list1 = x.split()

a = " ".join(list1)

print(a)

. Given a string s consisting of words and spaces, return the length of the last word in the string.

s = ‘fly me to the moon’
s = s.split()
print(f’the last word {s[-1]} with length ',len(s[-1]))

2.Write a python program to print the cube of all numbers from 1 to a given number

num = int(input("entre a number : "))
for i in range(1,num+1):
i=i**3
print(i,end = ’ ')

3.Write a Python program to count the number of characters (character frequency) in a string.

string = ‘googlecom’
dict = {}
for i in string:
dict[i] = string.count(i)
print(dict)

4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.

string = ‘hussain’
if len(string) >=2:
print(string[0:2]+string[-2:])
else:
print(“empty string”)
5.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

s1, s2= ‘abc’, ‘xyz’
a,b =s1[2],s2[2]
print ((s2[0:2]+a +" "+ s1[0:2]+b))

  1. finding the length of the word in the given line
    l=" fly me to the moon"

lst=l.split()

print(‘Length of the word is’, len(lst[4]))

#1. Given a string s consisting of words and spaces, return the length of the last word in the string.

s = “fly me to the moon”
last = s.split()
print(f’length of last word of a string: ',len(last[-1]))

#2.Write a python program to print the cube of all numbers from 1 to a given number

num = int(input("Enter a number: "))
for i in range(1,num+1):
print(i**3)

#3.Write a Python program to count the number of characters (character frequency) in a string.

string = “googlecom”
d = {}
for i in string:
d[i] = string.count(i)
print(d)

#4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string.

If the string length is less than 2, return the empty string instead

string = input("Enter a string: ")
if len(string) >=2:
print(string[0:2]+string[-2:])
else:
print(“Empty string”)

1. Given a string s consisting of words and spaces, return the length of the last word in the string.

x=input("Enter the String: ")
l=x.split()
print(“The length of last word in the string is:”,len(l[-1]))

2.Write a python program to print the cube of all numbers from 1 to a given number.

x=int(input("Enter a number: “))
for i in range(1,x+1):
print(i**3,end=” ")

3.Write a Python program to count the number of characters (character frequency) in a string.

x=input("Enter the String: ")
d={}
for i in x:
d[i]=x.count(i)
print(d)

4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.

x=input("Enter the String: ")
if len(x)<2:
print(“Empty String”)
else:
print(x[:2]+x[-2:])

5.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

x=input("Enter the 1st String: ")
y=input("Enter the 2nd String: “)
r_x=x[2:]
r_y=y[2:]
print(y.replace(y[2:],r_x)+” "+x.replace(x[2:],r_y))

Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.
Ans:
def string_first_last(string):
if len(string) < 2:
return ‘’
return string[0:2] + string[-2:]
print(string_first_last(‘hello’))
print(string_first_last(‘w3’))
print(string_first_last(‘w’))

Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

str1=str(input(“Enter the first string:”))
str2=str(input(“Enter the second string:”))
def swap1_swap2(str1,str2):
pass
return “”
w1 = str2[:2] + str1[2:]
w2 = str1[:2] + str2[2:]
print(“After Swapping two strings:”,w1,w2)
swap1_swap2(str1,str2)

Given a string s consisting of words and spaces, return the length of the last word in the string.

s=str(input(“Enter a sentence:”))
def len_word(s):
pass
lis = list(s.split(" "))
return len(lis[-1])
print(“The length of last word is”,len_word(s))

  1. s=input(“enter the string”)
    a=s.split()
    print(len(a[-1]))

  2. n=int(input(“enter the input”))
    for i in range(1,n+1):
    print(i**3)

  3. s=input(“enter the string”)
    if len(s)<2:
    print(“”)
    else:
    print(s[0:2:1]+s[-2::1])

  1. finding the length of the word in the given line
    l=" fly me to the moon"

lst=l.split()

print(‘Length of the word is’, len(lst[4]))

  1. python program to print the cube of all numbers
    def cube(n):
    for i in range(1, n+1):
    cube = i ** 3
    print(f"The cube of {i} is: {cube}")
    number = int(input("Enter a number: "))
    cube(number)

  2. Python program to count the number of characters
    string = “googlecom”
    freq = {}
    for i in string:
    freq[i] = string.count(i)
    print(freq)

  3. Python program to get a string made of the first 2 and last 2 characters of a given string
    def chars(string):
    if len(string) < 2:
    return “”
    else:
    return string[:2] + string[-2:]
    given = input("Enter a string: ")
    result = chars(given)
    print(“Result:”, result)

  4. Python program to get a single string from two given strings, separated by a space and swap the first two characters

string1 = input("Enter a string1: ")

first_char1=string1[0:2]

string2 = input("Enter a string2: ")

first_char2=string2[0:2]

first=string1.replace(first_char1,first_char2)

second=string2.replace(first_char2,first_char1)

x = first+" "+second

list1 = x.split()

result = " ".join(list1)

print(“Enter the Swap result :”,result)

num = int(input("Enter the end of range you want the cube of: "))

for i in range(1,num+1):
i = i**3
print(i,end=‘,’)

Write a python program to print the cube of all numbers from 1 to a given number.
Num=int(input(“Enter a number:”))

Cube=NumNumNum

print(“The cube of a given number is:”, Cube)

Write a python program to print the cube of all numbers from 1 to a given number.

Num=int(input(“Enter a number:”))
def cube(Num):
pass
return “”
Num=Num**3
print(“The Cube of a number is:”,Num)
cube(Num)

Write a Python program to count the number of characters (character frequency) in a string.

string=str(input(“Enter a string:”))
char = {}
for s in string:
if s in char:
char[s] += 1
else:
char[s] = 1
print(“Count of characters in the string is:”,char)

1. Given a string s consisting of words and spaces, return the length of the last word in the string.

s=input(“enter the String:”)

l=s.split(" ")

print(“last word length:”,len(l[-1]))

4. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the string length is less than 2, return the empty string instead.

s=input(“Enter the string:”)

if len(s)>=2:

print(s[0:2]+s[-2:])

else:

print(“Empty String”)

2.Write a python program to print the cube of all numbers from 1 to a given number.

n= int(input())
for i in range(1,n+1):
c=i**3
print(c)

3.Write a Python program to count the number of characters (character frequency) in a string.

Sample String : googlecom

Expected Result : {‘g’: 2, ‘o’: 3, ‘l’: 1, ‘e’: 1, ‘c’: 1, ‘m’: 1}

n=input("Enter the String: ").lower()
s={}
for i in n:
if i in s:
s[i]+=1
else:
s[i]=1
print(s)

5.Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

Sample String : ‘abc’, ‘xyz’

Expected Result : ‘xyc abz’

s1=input(“enter strin1:”)

s2=input(“enter strin2:”)

print(s2+" "+s1)

#1

s = " fly me to the moon "

t=s.split()

print(len(t[-1]))

#2

num=int(input("Enter a number: "))

for i in range(1,num+1):

print(i**3)

#3

str=“googlecom”

dict={}

for i in str:

dict[i]=str.count(i)

print(dict)

#4

str=“interesting”

if len(str)<2:

print()

else:

print(str[0:2]+str[len(str)-2:len(str):1])

#5

x,y=“abc”,“xyz”

a=x[0:2]

b=y[0:2]

x=b+x[2]

y=a+y[2]

p=f"{x} {y}"

print(p)