#!/usr/bin/env python
import os
src_str = raw_input("input string:")
def str_loop(mystr):
if not isinstance(mystr, str):
print "not string"
return ""
str_len = len(mystr)
for i in xrange(str_len):
#mystr = mystr[1:] + mystr[0]
#把 str[0] 跟 str[1:] 分开,就成为 "1" + "234" , 把str[0]放要尾部就成了
mystr = ("%s%s" % (mystr[1:] , mystr[0])) #第2个字节到尾部的字符串 加 第一个字符串 就掉换成功了
print "type(mystr) = " + repr(type(mystr))
print "str = %s" % (mystr, )
if __name__ == "__main__":
str_loop(src_str)
[axlrose@mygentoo strtest] $ ./strtest.py
input string:1234567890
type(mystr) = <type 'str'>
str = 2345678901
type(mystr) = <type 'str'>
str = 3456789012
type(mystr) = <type 'str'>
str = 4567890123
type(mystr) = <type 'str'>
str = 5678901234
type(mystr) = <type 'str'>
str = 6789012345
type(mystr) = <type 'str'>
str = 7890123456
type(mystr) = <type 'str'>
str = 8901234567
type(mystr) = <type 'str'>
str = 9012345678
type(mystr) = <type 'str'>
str = 0123456789
type(mystr) = <type 'str'>
str = 1234567890
str="1234" --> "2341" --> "3412" -> "4123" -> "1234" 这种字符串移动
思路是 原str="1234" -- 把 str[0] 跟 str[1:] 分开,就成为 "1" + "234" , 把str[0]放要尾部就成了
str[1:] + str[0] -->> "234" + "1" = "2341"
("%s%s" % (str[1:], str[0])) --> 因为考虑到这种方式比 str = str[1:] + str[0] 方式性能更好些,所以...
>>> from string import Template
>>> s = Template('${head}${tail}')
>>> str="1234"
>>> str=s.substitute(head=str[1:] , tail=str[0])
>>> print str
>>>'2341'