Use syntax string[x:y] to slice a string starting from index x up to but not including the character at index y. If you want only to cut the string to length in python use only string[: length].
Python cut the string to length Example
Simple example code. Cut the string by 5 lengths. If index
x is not specified it defaults to zero.
text = "Hello Python developer" cut_str = text[:5] print(cut_str)
Cut a string starting from index 5 to 10
text = "Hello Python developer" cut_str = text[5:10] print(cut_str)
Do comment if you have any doubts and suggestions on this Python string topic.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.