Write a Python program which gets from the keyboard which is made the first 2 and the last 2 characters. If the length of the string is less than 2, then the program should return a value. Otherwise, it should return an empty string.

Python / Data Types in Python

319

Program:

main.phy
----------

import sys
def main ():
main ()

TypeCast.py
-------------
def strings_onboth_ends(str):
    if len(str) < 2:
     return ""
    return str[0:2] + str[-2:]

print(strings_onboth_ends ("w3resource"))
print(strings_onboth_ends ("w3"))
print(strings_onboth_ends ("w"))


Output:

w3ce
w3w3

Explanation:


This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.