Python Forum
from string to list to string.
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from string to list to string.
#1
def splice():
    name = raw_input('Enter name and age.\n> ')
    result = name.split()
    add_comma = result[:1] + [', '] + result[1:]
    new = ''.join(add_comma)
    print (new)

splice()
input: John 56
output: John, 56

Is there a better way to do this?
Reply
#2
name = 'John 56'
name = name.replace(' ', ', ', 1)
print(name)
result:
Output:
'John, 56'
Reply
#3
Alternative:
>>> name = 'John 56'
>>> ', '.join(name.split())
'John, 56'

>>> # As Larz60+ suggest also work without 1
>>> name.replace(' ', ', ')
'John, 56'
Reply
#4
Thanks for the help!

I should have asked it this way first but how would I get an output of John Doe, 56 from John Doe 56?
Reply
#5
split it up into many white-space separated words (however many there are), and take the last item [-1] as the age and slice all but the last [:-1] one as the name.  suggestion: use a variable name other than name for the input before the name is sliced out.  extra points: check if the last one is a valid age (is '56.1' a valid age?)  more points: take ages like '27 years and 3 months'.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
>>> help(str.rsplit)
Help on method_descriptor:

rsplit(...)
   S.rsplit(sep=None, maxsplit=-1) -> list of strings
   
   Return a list of the words in S, using sep as the
   delimiter string, starting at the end of the string and
   working to the front.  If maxsplit is given, at most maxsplit
   splits are done. If sep is not specified, any whitespace string
   is a separator.
Reply
#7
str.join() is faster than replace()... I think
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Feb-04-2017, 01:26 PM)wavic Wrote: str.join() is faster than replace()... I think
Replace is faster,there is 1 more function call in(join() + split()).
About 3-sec faster in a 10000000 timeit loop.

Now do this not matter all for this task,
and his last requirement destroy it for replace()  Doh
Quote:would I get an output of John Doe, 56 from John Doe 56?
That's why i did hint about rsplit().
Reply
#9
regex also works fine.  But of the various things posted, I would prefer ", ".join(spam.split())


>>> import re
>>> spam = "can you feel it coming in the air tonight"
>>> re.sub(r"\s+", ", ", spam)
'can, you, feel, it, coming, in, the, air, tonight'
Reply
#10
'Hold, On'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 274 Yesterday, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 341 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 691 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 2,182 Feb-13-2025, 07:08 AM
Last Post: michaeljordan
  question about changing the string value of a list element jacksfrustration 4 2,176 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,210 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] Sub string not found in string ? jehoshua 4 1,444 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  extracting from a string Stephanos 6 1,231 Oct-01-2024, 06:52 AM
Last Post: DeaD_EyE
  Unable to understand the function string.split() Hudjefa 8 2,627 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
Question [SOLVED] How to replace characters in a string? Winfried 2 1,084 Sep-04-2024, 01:41 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020