Solved

How do I mirror text in Python?

Posted on 2011-02-22
7
1,939 Views
Last Modified: 2012-05-11
Mirroring the string 'COMPUTER' should result in the string 'COMPPMOC'.
Mirroring the string 'PYTHON' should result in 'PYTTYP'.

How do you do this in python??
0
Comment
Question by:mille109
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:xRalf
ID: 34956040
Hello mille109,

you can try this.
from math import floor
import sys
s = sys.argv[1]

middle = int(floor(len(s)/2))
for i in range(middle):
        print s[i],

if len(s) % 2 == 1:
        print s[i+1],
for i in range(middle):
        print s[-i + middle - 1],

Open in new window

0
 

Author Comment

by:mille109
ID: 34956303
how would you do this and be able to take text as input parameter??
0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 34958400
Here is your function

def mirror(s):
    s = s[:len(s)//2]                # take the first half of the string
    return s + ''.join(reversed(s))  # concatenate with the reversed string
    
    
print mirror('COMPUTER')
print mirror('PYTHON')

Open in new window


The len(s) returns the length of the content (here of the string). The // operators is the division that returns integer. The s[:n] is the slice syntax for geting the substring with characters from index 0 to n (n excluded).

The reversed(s) takes any sequence (here the string of characters) and returns reversed iterator. The .join() joins the iterated sequence by its given string -- here the empty one.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:zaghaghi
ID: 35038302
Hi,
I recommend pepr solution, it's clear and correct.
0
 
LVL 6

Expert Comment

by:xRalf
ID: 35038630
>> how would you do this and be able to take text as input parameter??

If you write the code into the file mirror.py, you can use the program as follows:
python mirror.py computer
python mirror.py python

I agree that pepr's solution is clear and nice.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 36515794
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question