5 Replies - 437 Views - Last Post: 28 January 2014 - 09:30 PM Rate Topic: -----

#1 i4m5up3rm4n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 14

How do you remove spaces from the output?

Posted 28 January 2014 - 08:57 PM

I'm using Python 2.7.6

I'm trying to write a Python code that will display the output as all CAPS with a period in between each character and after the last. no spaces.

Here is my code:

initials = raw_input("What are your initials? ")

print initials[0].upper(),".",initials[1].upper(),".",initials[2].upper(),"."


Let's say the users initials are 'ajl'

The ending output after user has input their initials is:

Output: A . J . L .

How do I get it do output with no spaces between the characters so it looks like: A.J.L.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How do you remove spaces from the output?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: How do you remove spaces from the output?

Posted 28 January 2014 - 09:03 PM

Try the join function:

>>> somelist = ['a','b','c']
>>> ".".join(somelist) 
a.b.c

Was This Post Helpful? 1
  • +
  • -

#3 i4m5up3rm4n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 14

Re: How do you remove spaces from the output?

Posted 28 January 2014 - 09:14 PM

View Postjon.kiparsky, on 28 January 2014 - 09:03 PM, said:

Try the join function:

>>> somelist = ['a','b','c']
>>> ".".join(somelist) 
a.b.c



How would I add that to my current code to get to output properly?
Would I have to change the way my current code is written?
I'm not creating a static input, I am asking for a user input of their initials.
So if the user inputs "ajl", how would I write the code to change it to an output of "A.J.L."?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: How do you remove spaces from the output?

Posted 28 January 2014 - 09:20 PM

Okay, you're really going to have to put forth a little bit of effort here. Look at that example and try to figure out what the join function is doing. If you're still not sure, ask the interpreter:

>>> help("".join)


If that doesn't work, take a look at the documentation (at python.org), and try to determine what the str.join function can do.
Was This Post Helpful? 0
  • +
  • -

#5 i4m5up3rm4n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 14

Re: How do you remove spaces from the output?

Posted 28 January 2014 - 09:25 PM

I figured it out. The join command works, I just had to fiddle with how I wrote the code as a whole.

initials = raw_input("What are your initials? ")

initUpper = initials[0].upper(),".",initials[1].upper(),".",initials[2].upper(),"."

initJoined = "".join(initUpper)

print initJoined



This worked for me, but I was wondering if anyone knew of an easier or quicker way of having Python do this?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: How do you remove spaces from the output?

Posted 28 January 2014 - 09:30 PM

Here's one useful trick: the list() function converts a string to a list of characters.

>>> list("foo")
['f', 'o', 'o']



(the join function, called on the empty string, is really the inverse of this)

Here's another: many (not all) functions on strings do not modify the string they're called on, but instead return a value. So you can pass the result of one call directly into another function:

>>> list("foo".upper())
['F', 'O', 'O']



That should more or less get you there.

This post has been edited by jon.kiparsky: 28 January 2014 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1