GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Feb-2009, 17:52
804 804 is offline
New Member
 
Join Date: Feb 2009
Posts: 1
804 is on a distinguished road

Shifting characters in a string?


I've just started to learn how to use python and I'm trying to define a function in python that could shift each characters in a string with a random number.

I was given a hint that using looping would be helpful to complete this exercise. But I have no clue how to manipulate every character in a string.

Python Code:
def shiftStringBy(s,n) :
   s2 = "" 
   for i in range (len(s)) :
     ord(s[i]) + n
    

That's as far as I got, but I know its totally incorrect. Can anybody please help me?
Last edited by admin : 28-Feb-2009 at 13:43. Reason: Please insert your example Python codes between [PY] and [/PY] tags
  #2  
Old 27-Feb-2009, 20:40
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Shifting characters in a stirng?


As I understand it, you want to shift the characters in a string by x numbers to the right, e.g. "abcd" shifted by 2 becomes "cdab". Is this correct?

What is "s2"? You define it as an empty string yet you never use it. Are you using a user input to determine the random value? You don't have anything to receive that input from the user. (However, if you are simply calling the function manually then you're fine).

I don't think you want to be using the ord function. According to the Python documentation, it returns an integer value of the Unicode character.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Code to Reverse words in a string lief480 C++ Forum 7 16-Oct-2008 09:51
Determine the number of words and characters madpotential Python Forum 3 26-Feb-2008 21:12
Need help printing "Hello" 7 characters high and 5 characters wide Emmy C++ Forum 4 15-Aug-2007 08:28
Re: Things to Avoid in C/C++ -- gets() , Part 1 WaltP C Programming Language 5 21-Jun-2007 13:13
fast characters in console svhb C Programming Language 10 22-Jun-2005 08:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.