当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

以不同的方式反转字符串

  1. test_string = 'Python Programming'
  2. string_reversed = test_string[-1::-1]
  3. print(string_reversed)
  4. string_reversed = test_string[::-1]
  5. print(string_reversed)
  6. # String reverse logically
  7. def string_reverse(text):
  8. r_text = ''
  9. index = len(text) - 1
  10. while index >= 0:
  11. r_text += text[index]
  12. index -= 1
  13. return r_text
  14. print(string_reverse(test_string))

Output:

  1. gnimmargorP nohtyP
  2. gnimmargorP nohtyP
  3. gnimmargorP nohtyP

该分类下的相关小册推荐: