0
Write a python program to get a string and will output the first 3 and the last 3 chars from the given string.
If the string length is less than 3, return instead of the empty string. * The user is responsible for input Please help me to solve this. Thank you!
4 odpowiedzi
+ 2
Use slicing.
0
How? Sorry, I'm really a newbie.
0
Can anyone help me how to input this problem please
- 1
I agree! Reading up on slicing is definitely worth it but basically, if you have a string = “abcdefgh” for example. You can get the first two characters if you do string[0:2]. The characters inside the string start by being indexed (or labelled) at 0. So, ‘a’ is indexed (or labelled) at 0. ‘b’ is indexed at 1 etc. When you put the variable name and then use square brackets with a colon, you are slicing the string and you use the particular indeces. It is in the format variable_name[starting index: ending index]. Just be careful because it will slice the part of the string up to but not including the ending index. So in our previous example, string[0:2] would output “ab” but not “c” even though c is indexed at 2. You can also use negative indeces to signify the end of a string and add skips but I will leave that for you to read up on! Good luck! Julius Poloyapoy