0

How can I remove the final characters from a string in python until I reach a certain amount of characters?

How can I turn: abcdefghijklmnopqrstuvwxyz => abcdefghijklmnopqr; using python?

ArthurIsNo
  • 29
  • 1
  • "reach a certain number of characters"? Is a certain length left (`string[:length]`) or deleted (`string[:-length]`)? – Mechanic Pig Oct 05 '22 at 04:22

2 Answers2

0

Try string slicing.

You can use negative indexing:

'abcdefghijklmnopqrstuvwxyz'[:-8]

or positive:

'abcdefghijklmnopqrstuvwxyz'[:18]

Pick your poison

smac89
  • 39,374
  • 15
  • 132
  • 179
0

You can treat the string as an array of symbols

c_1 = "abcdefghijklmnopqrstuvwxyz"
l = 18
c_2 = c_2[:l]
Raibek
  • 558
  • 3
  • 6