0

name = "Python"

I want to split the variable name into 'Pyt' and 'hon' using the split method.

I have achieved this with name[0:3] and name[3:] and saving both of them in different variables but can it be done using a method ?

Mathew
  • 11
  • 2
  • 1
    You mean split a string in half? Why not `name[:len(name)//2]` and `name[len(name)//2:]` (adjust parity to taste)? I don't think the dupe is related, BTW, but I voted to close as unclear. – ggorlen Jan 09 '20 at 19:07
  • Do you mean a method to split strings in half? The `split` method splits strings on a specific character, but its unclear how you wanted to split up your string in a generic way. – ekneiling Jan 09 '20 at 19:07
  • What do you mean with "using a method"? Can you give us an example of what you want to do? – enbermudas Jan 09 '20 at 19:08
  • If you want to use a method, `name.__getitem__(slice(3))`, `name.__getitem__(slice(3, None))` – Nick T Jan 09 '20 at 19:11
  • If my string is Python I want to split the string into two but I get to choose what I want to split and it should retain the character on what I am splitting on – Mathew Jan 09 '20 at 19:13

0 Answers0