Python Forum
Last caracter of a string truncated issue when working from the end of the string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Last caracter of a string truncated issue when working from the end of the string
#1
Hello, can someone please explain why when I select a range starting from the end of a string it truncates the last character?
I can work from the front end as variable dst will always be the same length but when faced with an unknown string length it would be an issue

dst = (r"C:\Users\tekno\Documents\Client Drawings\AAA001")
dwg_name = (dst[-7:-1])
print(dst)
print(dwg_name)
print(dst[40:47])
print(dst[-7:-1])
print(dst[-1])
Output:
C:\Users\tekno\Documents\Client Drawings\AAA001 \AAA00 \AAA001 \AAA00 1
Reply
#2
Why the parenthesis in the first two lines? They do nothing.

Slices work just like the for loop. They start at the start and end just before the end. To slice all the way to the end don't specify an end.
dwg_name = dst[-7:]
Same goes for slicing from the start.
dwg_name = dst[:7]
Teknohead23 likes this post
Reply
#3
Rather than [-7:-1] to go to the end [-7:] -7 and the rest.
dst = (r"C:\Users\tekno\Documents\Client Drawings\AAA001")
dwg_name = dst[-7:]
print(dst)
print(dwg_name)
Output:
C:\Users\tekno\Documents\Client Drawings\AAA001 \AAA001
Teknohead23 likes this post
Reply
#4
Use pathlib then it become much more flexible.
>>> import pathlib
>>> 
>>> dst = r"C:\Users\tekno\Documents\Client Drawings\AAA001"
>>> folder_last = pathlib.PurePath(dst).name
>>> folder_last
'AAA001'
>>> 
>>> # Join would be
>>> my_path = pathlib.Path('C:\\')
>>> my_path.joinpath(folder_last)
WindowsPath('C:/AAA001')
>>> pathlib.Path.home().joinpath(folder_last)
WindowsPath('C:/Users/Tom/AAA001')
Teknohead23 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Getting python's default 'printed' byte-string as string ? MvGulik 8 136 Apr-06-2026, 09:16 AM
Last Post: Dustbunny
  Printing a string containing newlines meijeru 3 69 Mar-23-2026, 03:20 AM
Last Post: deanhystad
  python re.finditer returns a null string when expecting a None result arbiel 8 802 Jan-11-2026, 02:57 AM
Last Post: Pedroski55
  Spaces in string Mallard 9 1,071 Dec-19-2025, 01:04 PM
Last Post: Mallard
  Convert any Python expression to a string voidtrance 2 1,130 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,524 May-28-2025, 07:56 AM
Last Post: Winfried
  Return a string or byte object from Enum class? Calab 5 1,959 May-14-2025, 05:21 PM
Last Post: snippsat
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,673 May-01-2025, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 1,800 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 1,412 Feb-14-2025, 12:23 AM
Last Post: deneme2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020