【问题标题】:Python & Strings/Loops help please [closed]Python和字符串/循环请帮助[关闭]
【发布时间】:2015-07-29 09:35:02
【问题描述】:

我想弄清楚如何制作一个输入的名称:

full_name = input('Enter your full name ')

通过使用 while 循环将其更改为完全倒退。

例如:将Jenny King改为gniK ynneJ

提前致谢:)

【问题讨论】:

  • 这听起来不像是一本好书,老实说。当您可以使用切片或reversed 时,为什么要使用 while 循环来反转字符串?既然可以使用split,为什么还要使用切片将字符串拆分为名字和姓氏?如果您遇到困难,那可能只是因为他们让您以迂回的方式完成作业。
  • 当我阅读我的书时,我想知道为什么他们也为此使用 while 循环,并且它没有解释如何以这种方式使用 while 循环...
  • @Kevin 我更担心因为“将 ... 分配给字符串”。也许这不是一本 Python 书。
  • @Kevin 可能是作者正在经历“其他语言如何做到这一点”,然后“看看用 Python 做有多容易”?这可能不是最有效的风格,但是,嘿,这本书也不是我写的……

标签: python


【解决方案1】:

1- 使用while循环

i = len(full_name.split()) - 1
while(i >= 0):
    print full_name.split()[i]
    i = i-1

2- 切片

x = full_name.index(" ")
new_string = full_name[x+1:] + " " + full_name[0:x]
print new_string

3- 上下计数

lower = 0
upper = 0
for letter in full_name:
    if letter.islower():
        lower = lower + 1
    elif letter.isupper():
        upper = upper + 1
print "number of lower letters = %s" % lower 
print "number of upper letters = %s" % upper 

4-纠正句子

bad_string = "You have to be vewy, vewy twicky to twap a wascally wabbit"
correct_string = bad_string.replace('w', 'r')
print correct_string

【讨论】:

  • @JennyKing,如果您要对问题进行改进,请编辑问题,而不是其他人试图回答您的问题。谢谢。
  • 对不起。不是故意的。新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2015-07-30
  • 1970-01-01
相关资源
最近更新 更多