Closed Thread
Results 1 to 4 of 4

Thread: Splitting Strings with Empty Separator (Python)

  1. #1
    Matt's Avatar
    Matt is offline Learning Programmer
    Join Date
    Jan 2007
    Location
    Northwestern US
    Posts
    47
    Rep Power
    19

    Splitting Strings with Empty Separator (Python)

    I have been playing around with Python lately, and there is one operation that I have yet to figure out how to do. This is splitting a string into its individual characters, which I used to be able to do with other languages by simply splitting with an empty separator. In python, however, this returns an error.

    Code:
    >>> "testing123".split("")
    Traceback (most recent call last):
      File "<pyshell#14>", line 1, in ?
        "testing123".split("")
    ValueError: empty separator
    How can I do this simple operation? Thanks...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I don't remember if there's such function (I'm a bit rusty), but it can be done in two lines, using a for-loop.

    This shows how, using the interpreter:
    Code:
    >>> the_string = "Hello, World!"
    >>> the_list   = []
    >>> for each_character in the_string:
    	the_list.append(each_character)
    
    	
    >>> the_list
    ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
    >>>

  4. #3
    Matt's Avatar
    Matt is offline Learning Programmer
    Join Date
    Jan 2007
    Location
    Northwestern US
    Posts
    47
    Rep Power
    19
    Great, that works just fine. Thanks.

  5. #4
    paddy3118 is offline Newbie
    Join Date
    Apr 2007
    Location
    UK
    Posts
    7
    Rep Power
    0
    Try:
    list("Any string")

    - Paddy.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. splitting
    By SpaceCowboy in forum C and C++
    Replies: 2
    Last Post: 12-01-2010, 04:58 PM
  2. [PYTHON] Replace strings in all files in directory and subdirectories
    By ZekeDragon in forum Classes and Code Snippets
    Replies: 7
    Last Post: 09-02-2009, 06:35 PM
  3. Splitting Error
    By policy in forum Perl
    Replies: 2
    Last Post: 09-21-2007, 10:13 AM
  4. Splitting...
    By Chan in forum C# Programming
    Replies: 3
    Last Post: 08-06-2007, 05:41 AM
  5. Splitting by plus (+)?
    By Cosmet in forum Perl
    Replies: 4
    Last Post: 05-21-2007, 05:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts