Slicing Text With slice()

With .at(-1) in place of title[-1], the port of ticket_branch.py now spots the full stop at the end of PAY-2304’s title. The helper’s next lines want more than one character, though. The review bot accepts commit subjects of at most 50 characters, and the time sheet wants the ticket number.

In Python, the subject is a slice, [:50], and the ticket number can be one too. JavaScript’s brackets hold one position and nothing more, so a slice needs a new spelling. Once you have it, your Python intuition does the rest. Start with the commit subject.

Cut the Commit Subject to 50 Characters

The Python helper builds the subject with an f-string and slices it to 50 characters in one line. Here it is with PAY-2291’s key and title:

Language: Python
key = "PAY-2291"
title = "Refund emails show the wrong currency: EUR shown as USD"
subject = f"{key}: {title}"[:50]
print(f"Commit: {subject}")

JavaScript has no [a:b] syntax, so you use .slice() to select a range. This method takes the start and the end as arguments. Here’s the port, with a template literal standing in for the f-string:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.