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
  How do I parse the string? anna17 4 260 Apr-10-2024, 10:26 AM
Last Post: DeaD_EyE
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  remove gilberishs from a "string" kucingkembar 2 261 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Matching string from a file tester_V 5 433 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  Python3 string slicing Luchano55 4 602 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Retrieve word from string knob 4 481 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 930 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  Virtual Env changing mysql connection string in python Fredesetes 0 372 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Comparing Dataframe to String? RockBlok 2 404 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Sample random, unique string pairs from a list without repetitions walterwhite 1 448 Nov-19-2023, 10:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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