Python: Get a string made of the first 2 and the last 2 chars from a given a string
Get string of first and last 2 chars.
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 Solution:
Python Code:
Sample Output:
w3ce w3w3
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to return a string made of the first two and the last two characters of a given string using slicing.
- Write a Python program to implement the extraction of the first two and last two characters recursively.
- Write a Python program to use conditional expressions to return the concatenated substring if the string length is at least 2, else return an empty string.
- Write a Python program to check the length of a string and output a new string composed of its first and last two characters using a ternary operator.
Go to:
Previous: Write a Python program to count the number of characters (character frequency) in a string.
Next: Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself.
Python Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Based on 68 votes, average difficulty level of this exercise is Hard
.
Test your Programming skills with w3resource's quiz.