-3

Say I have a string that is a sentence, i.e. text = 'Say I have a string that is a sentence' ; is there a method that can be called on text to split the assigned value for string into individual characters, so a list of each individual index I suppose?

  • https://www.geeksforgeeks.org/python-split-string-into-list-of-characters/ and https://www.geeksforgeeks.org/python-splitting-string-to-list-of-characters/ – Kempie Oct 05 '20 at 20:11
  • @Kempie: That seems like a strange way to do it. In what way would using a loop be better than using `list(text)`? – Opifex Oct 05 '20 at 20:14
  • 4
    Does this answer your question? [How to split a string into an array of characters in Python?](https://stackoverflow.com/questions/4978787/how-to-split-a-string-into-an-array-of-characters-in-python) – shreyasm-dev Oct 05 '20 at 20:17
  • @Opifex. Pasted the wrong link first but there is a few options now to choose from. – Kempie Oct 05 '20 at 20:25

3 Answers3

3

Your string already is a sequence of separate characters that can be indexed like you can with a list.

text = 'Say I have a string that is a sentence'
text[0]
>>> S
text[4]
>>> I

No need to use a fancy function for this.

But if you, for some reason need a variable of type List, you can use list(text).

list(text)
>>> ['S', 'a', 'y', ' ', 'I', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 't', 'h', 'a', 't', ' ', 'i', 's', ' ', 'a', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
Opifex
  • 362
  • 4
  • 15
  • Thanks very much; it seems I had a brain fart. – Alec Davis Oct 05 '20 at 20:28
  • @Alec Davis Feel free to accept the answer if this solved your problem. – Opifex Oct 05 '20 at 20:29
  • @Carcigenicate I updated the first sentence to be more technically correct. – Opifex Oct 05 '20 at 20:30
  • This question is a duplicate of [this one](https://stackoverflow.com/questions/4978787/how-to-split-a-string-into-an-array-of-characters-in-python). Questions as basic as this usually are duplicates. Please don't answer duplicates. Instead, flag them / vote to close. – Pranav Hosangadi Oct 05 '20 at 20:31
1

list the whole string straight away. drop an example next time

  answer = 'Is this your what you are talking about'

  list(anwser)
  #output
  'I', 's', ' ', 't', 'h', 'i', 's', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 't', 'a', 'l', 'k', 'i', 'n', 'g', ' ', 'a', 'b', 'o', 'u', 't']
Camue
  • 469
  • 7
  • 17
  • This question is a duplicate of [this one](https://stackoverflow.com/questions/4978787/how-to-split-a-string-into-an-array-of-characters-in-python). Questions as basic as this usually are duplicates. Please don't answer duplicates. Instead, flag them / vote to close. – Pranav Hosangadi Oct 05 '20 at 20:35
-1
string='Here is what you are looking for';   
print(list(string));
Sarun UK
  • 6,210
  • 7
  • 23
  • 48
  • 1
    Besides the code containing errors, this doesn't answer the question. `.split()` splits on whitespace. – Carcigenicate Oct 05 '20 at 20:27
  • `Print` or `print`? – eyllanesc Oct 05 '20 at 20:59
  • While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ This is _especially true_ if your answer has already been posted by someone else. – Pranav Hosangadi Oct 05 '20 at 21:08
  • So add an explanation and explain what is different from other answers. – 10 Rep Oct 06 '20 at 02:50