Solved

How do I mirror text in Python?

Posted on 2011-02-22
7
1,777 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
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 28

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now