Write a program to create a new string made of an input string’s first, middle, and last character.
Given:
str1 = "James"
Expected Output:
Jms
Code:
str1 = 'James'
print("Original String is", str1)
# Get first character
res = str1[0]
# Get string size
l = len(str1)
# Get middle index number
mi = int(l / 2)
# Get middle character and add it to result
res = res + str1[mi]
# Get last character and add it to result
res = res + str1[l - 1]
print("New String:", res)
Write a program to create a new string made of the middle three characters of an input string.
Case 1
str1 = "JhonDipPeta"
Output
Dip
Case 2
str2 = "JaSonAy"
Output
Son
Code:
Str1=input()
print("Original String is", str1)
# first get middle index number
mi = int(len(str1) / 2)
# use string slicing to get result characters
res = str1[mi - 1:mi + 2]
print("Middle three chars are:", res)
Count all letters, digits, and special symbols from a given string
Given:
str1 = "P@#yn26at^&i5ve"
Expected Outcome:
Total counts of chars, digits, and symbols
Chars = 8
, Digits = 3
Symbol = 4
Code:
sample_str = "P@yn2at&#i5ve"
print("total counts of chars, Digits, and symbols \n")
char_count = 0
digit_count = 0
symbol_count = 0
for char in sample_str:
if char.isalpha():
char_count += 1
elif char.isdigit():
digit_count += 1
# if it is not letter or digit then it is special symbol
else:
symbol_count += 1
print("Chars =", char_count, "Digits =", digit_count, "Symbol =", symbol_count)
Write a program to find all occurrences of “USA” in a given string ignoring the case.
Given:
str1 = "Welcome to USA. usa awesome, isn't it?"
Expected Outcome:
The USA count is: 2
str1 = "Welcome to USA. usa awesome, isn't it?"
sub_string = "USA"
# convert string to lowercase
temp_str = str1.lower()
# use count function
count = temp_str.count(sub_string.lower())
print("The USA count is:", count)
Given a string s1, write a program to return the sum and average of the digits that appear in the
string, ignoring all other characters.
Given:
str1 = "PYnative29@#8496"
Expected Outcome:
Sum is: 38 Average is 6.333333333333333
Code:
input_str = "PYnative29@#8496"
total = 0
cnt = 0
for char in input_str:
if char.isdigit():
total += int(char)
Given:
str1 = "James"
Expected Output:
Jms
Code:
str1 = 'James'
print("Original String is", str1)
# Get first character
res = str1[0]
# Get string size
l = len(str1)
# Get middle index number
mi = int(l / 2)
# Get middle character and add it to result
res = res + str1[mi]
# Get last character and add it to result
res = res + str1[l - 1]
print("New String:", res)
Write a program to create a new string made of the middle three characters of an input string.
Case 1
str1 = "JhonDipPeta"
Output
Dip
Case 2
str2 = "JaSonAy"
Output
Son
Code:
Str1=input()
print("Original String is", str1)
# first get middle index number
mi = int(len(str1) / 2)
# use string slicing to get result characters
res = str1[mi - 1:mi + 2]
print("Middle three chars are:", res)
Count all letters, digits, and special symbols from a given string
Given:
str1 = "P@#yn26at^&i5ve"
Expected Outcome:
Total counts of chars, digits, and symbols
Chars = 8
, Digits = 3
Symbol = 4
Code:
sample_str = "P@yn2at&#i5ve"
print("total counts of chars, Digits, and symbols \n")
char_count = 0
digit_count = 0
symbol_count = 0
for char in sample_str:
if char.isalpha():
char_count += 1
elif char.isdigit():
digit_count += 1
# if it is not letter or digit then it is special symbol
else:
symbol_count += 1
print("Chars =", char_count, "Digits =", digit_count, "Symbol =", symbol_count)
Write a program to find all occurrences of “USA” in a given string ignoring the case.
Given:
str1 = "Welcome to USA. usa awesome, isn't it?"
Expected Outcome:
The USA count is: 2
str1 = "Welcome to USA. usa awesome, isn't it?"
sub_string = "USA"
# convert string to lowercase
temp_str = str1.lower()
# use count function
count = temp_str.count(sub_string.lower())
print("The USA count is:", count)
Given a string s1, write a program to return the sum and average of the digits that appear in the
string, ignoring all other characters.
Given:
str1 = "PYnative29@#8496"
Expected Outcome:
Sum is: 38 Average is 6.333333333333333
Code:
input_str = "PYnative29@#8496"
total = 0
cnt = 0
for char in input_str:
if char.isdigit():
total += int(char)