Solved

How do I mirror text in Python?

Posted on 2011-02-22
7
1,805 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 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 …

815 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

11 Experts available now in Live!

Get 1:1 Help Now