Created
August 8, 2016 08:03
-
-
Save brainyfarm/9761efeeddec4e23bf69d10e950f79e6 to your computer and use it in GitHub Desktop.
FreeCodeCamp: Truncate a String (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). | |
Return the truncated string with a ... ending. | |
Note that inserting the three dots to the end will add to the string length. | |
However, if the given maximum string length num is less than or equal to 3, | |
then the addition of the three dots does not add to the string length in determining the truncated string. | |
""" | |
def truncate(string, num): | |
if(len(string) <= num): | |
return string | |
elif(len(string) > num): | |
if(num <= 3): | |
return string[:num] + '...' | |
return string[:num-3] + '...' | |
print truncate("A-tisket a-tasket A green and yellow basket", len("A-tisket a-tasket A green and yellow basket") - 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment