Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Python

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-2007, 10:23 AM
Matt's Avatar   
Matt Matt is offline
Learning Programmer
 
Join Date: Jan 2007
Location: Northwestern US
Posts: 47
Rep Power: 6
Matt is on a distinguished road
Default 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...
__________________
http://www.mattnichols.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-15-2007, 10:35 AM
v0id's Avatar   
v0id v0id is online now
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,453
Last Blog:
CherryPy(thon)
Rep Power: 27
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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', '!']
>>>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-15-2007, 10:40 AM
Matt's Avatar   
Matt Matt is offline
Learning Programmer
 
Join Date: Jan 2007
Location: Northwestern US
Posts: 47
Rep Power: 6
Matt is on a distinguished road
Default

Great, that works just fine. Thanks.
__________________
http://www.mattnichols.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-18-2007, 12:27 PM
paddy3118 paddy3118 is offline
Newbie
 
Join Date: Apr 2007
Location: UK
Posts: 6
Rep Power: 0
paddy3118 is on a distinguished road
Default

Try:
list("Any string")

- Paddy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings clookid PHP Tutorials 2 01-13-2007 03:23 PM


All times are GMT -5. The time now is 01:35 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
sam ........ 20.00000
gaylo565 ........ 18.00000
|pH| ........ 15.00000
WingedPanther ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads