C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program: We truncate the string to 3 and to 7 characters. If we use an out of range number like 100 the entire string is returned.
Python program that truncates string
value = "one two three"
# Truncate the string to 3 characters.
first = value[0:3]
print(first + ".")
# Truncate the string to 7 characters.
second = value[0:7]
print(second + ".")
Output
one.
one two.
Python program that uses short truncation syntax
letters = "abcdef"
# Omit the first 0 in the slice syntax.
# ... This truncates the string.
first_part = letters[:3]
print(first_part)
Output
abc