Python Koans Solution —— String
来源:互联网 发布:淘宝网网页版 编辑:程序博客网 时间:2024/06/03 21:39
Python Koans是一个学习Python编程语言的交互式教程,通过解决当中的问题来更深刻地理解Python。本系列文章为在此项目的学习中的问题解决方案及思考
Github Address: Python Koans Download Address
Lesson 2 String
字符串是 Python 中最常用的数据类型。我们可以使用引号’或”来创建字符串。
about_strings.py
#!/usr/bin/env python# -*- coding: utf-8 -*-from runner.koan import *class AboutStrings(Koan): def test_double_quoted_strings_are_strings(self): string = "Hello, world." self.assertEqual(True, isinstance(string, str)) def test_single_quoted_strings_are_also_strings(self): string = 'Goodbye, world.' self.assertEqual(True, isinstance(string, str)) def test_triple_quote_strings_are_also_strings(self): string = """Howdy, world!""" self.assertEqual(True, isinstance(string, str)) def test_triple_single_quotes_work_too(self): string = '''Bonjour tout le monde!''' self.assertEqual(True, isinstance(string, str)) def test_raw_strings_are_also_strings(self): string = r"Konnichi wa, world!" self.assertEqual(True, isinstance(string, str)) def test_use_single_quotes_to_create_string_with_double_quotes(self): string = 'He said, "Go Away."' self.assertEqual('He said, \"Go Away.\"', string) def test_use_double_quotes_to_create_strings_with_single_quotes(self): string = "Don't" self.assertEqual("Don\'t", string) def test_use_backslash_for_escaping_quotes_in_strings(self): a = "He said, \"Don't\"" b = 'He said, "Don\'t"' self.assertEqual(True, (a == b)) def test_use_backslash_at_the_end_of_a_line_to_continue_onto_the_next_line(self): string = "It was the best of times,\n\It was the worst of times." self.assertEqual(52, len(string)) def test_triple_quoted_strings_can_span_lines(self): string = """Howdy,world!""" self.assertEqual(15, len(string)) def test_triple_quoted_strings_need_less_escaping(self): a = "Hello \"world\"." b = """Hello "world".""" self.assertEqual(True, (a == b)) def test_escaping_quotes_at_the_end_of_triple_quoted_string(self): string = """Hello "world\"""" self.assertEqual("""Hello \"world\"""", string) def test_plus_concatenates_strings(self): string = "Hello, " + "world" self.assertEqual("Hello, world", string) def test_adjacent_strings_are_concatenated_automatically(self): string = "Hello" ", " "world" self.assertEqual("Hello, world", string) def test_plus_will_not_modify_original_strings(self): hi = "Hello, " there = "world" string = hi + there self.assertEqual("Hello, ", hi) self.assertEqual("world", there) def test_plus_equals_will_append_to_end_of_string(self): hi = "Hello, " there = "world" hi += there self.assertEqual("Hello, world", hi) def test_plus_equals_also_leaves_original_string_unmodified(self): original = "Hello, " hi = original there = "world" hi += there self.assertEqual("Hello, ", original) def test_most_strings_interpret_escape_characters(self): string = "\n" self.assertEqual('\n', string) self.assertEqual("""\n""", string) self.assertEqual(1, len(string))
阅读全文
0 0
- Python Koans Solution —— String
- Python Koans Solution —— Assert
- Python Koans Solution —— None
- Python Koans(1)-assert
- 一种学习语言的方式(koans系列-1)python koans
- Leetcode 8. String to Integer (atoi) The Solution of Python
- python—字符串string
- String Permutation java solution
- 模拟试——solution
- LeetCode434 Number of Segments in a String java and python solution
- Python Challenge 01 Solution
- Python Challenge 02 Solution
- Python Challenge 03 Solution
- Python Challenge 04 Solution
- Python Challenge 05 Solution
- Python Challenge 06 Solution
- Python Challenge 08 Solution
- Python Challenge 09 Solution
- 玲珑杯 1164
- jQuery方法原生实现---each遍历
- ArcEngine点集构三维面
- JSP九大内置对象和四个作用域
- 分布式和大型机优缺点
- Python Koans Solution —— String
- 编程练习
- 《Java编程思想》第7-10章
- Eclipse Jee Neon打开时报错 code=13的问题
- Linux内核链表总结
- 研究方向的一点思考
- CentOS 一键搭建 L2TP VPN 服务器
- 九度OJ题目1001:A+B for Matrices
- Win10 环境下安装配置 zsh