Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I mirror text in Python?

Posted on 2011-02-22
7
Medium Priority
?
2,024 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

715 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