Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45922 > unrolled thread
| Started by | Thomas Murphy <thomasmurphymusic@gmail.com> |
|---|---|
| First post | 2013-05-24 18:53 -0400 |
| Last post | 2013-05-24 20:24 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Prepending string "@" to usernames Thomas Murphy <thomasmurphymusic@gmail.com> - 2013-05-24 18:53 -0400
Re: Prepending string "@" to usernames Larry Hudson <orgnut@yahoo.com> - 2013-05-24 20:24 -0700
| From | Thomas Murphy <thomasmurphymusic@gmail.com> |
|---|---|
| Date | 2013-05-24 18:53 -0400 |
| Subject | Prepending string "@" to usernames |
| Message-ID | <mailman.2081.1369436041.3114.python-list@python.org> |
Hi beloved list,
I'm having a dumb and SO doesn't seem to have this one answered. I was
sent a long list of instagram usernames to tag for a nightlife
announcement in this format(not real names(i hope))
cookielover93
TheGermanHatesSaurkraut
WhatsThatBoy932834
I'd like to turn this raw text into a list and prepend the @ symbol to
the front of each one, so they're good to go for pasting without me
having to manually add the @ to each one.
Here's where I got to:
raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
address_library = [raw_address.split()]
print address_library
for address in address_library:
final_address = "@" + str(address)
print final_address
However my output is:
[['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']]
@['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']
I know I'm iterating wrong. May I ask how?
--
Sincerely,
Thomas Murphy
Code Ninja
646.957.6115
[toc] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2013-05-24 20:24 -0700 |
| Message-ID | <F-ydnVRrS4tMsz3MnZ2dnUVZ_sudnZ2d@giganews.com> |
| In reply to | #45922 |
On 05/24/2013 03:53 PM, Thomas Murphy wrote:
<snip>
> Here's where I got to:
>
>
> raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834"
> address_library = [raw_address.split()]
> print address_library
>
> for address in address_library:
> final_address = "@" + str(address)
> print final_address
>
>
> However my output is:
>
> [['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']]
> @['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']
>
>
> I know I'm iterating wrong. May I ask how?
>
> --
> Sincerely,
> Thomas Murphy
> Code Ninja
> 646.957.6115
>
No, you're not iterating wrong, but you do have two errors:
1: split() returns a list. You are putting this list (as a single element) inside a list.
Drop the square brackets. Make it: address_library = raw_address.split()
2: In your for loop, you want the print _inside_ the loop not outside. IOW, indent the print
line. The way you have it written it will only print the _last_ string.
-=- Larry -=-
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web