## 2019年10月27日日曜日

### Python - Using Methods - str class, methods(format, capitalize, find, islower, lower), f-string

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 7(Using Methods)、Exercise 9、10、11の解答を求めてみる。

コード

Python 3

```#!/usr/bin/env python3
from unittest import TestCase, main

class MyTestCase(TestCase):
def test9(self):
season = 'summer'
self.assertEqual('I love {}!'.format(season), 'I love summer!')
self.assertEqual(f'I love {season}!', 'I love summer!')

def test10(self):
side1 = 3
side2 = 4
side3 = 5
s = 'The sides have lengths 3, 4, and 5.'
self.assertEqual(
'The sides have lengths {}, {}, and {}.'.format(
side1, side2, side3),
s)
self.assertEqual(
f'The sides have lengths {side1}, {side2}, and {side3}.', s)

def test11a(self):
self.assertEqual('boolean'.capitalize(), 'Boolean')

def test11bc(self):
s = 'CO2 H2O'
first2 = s.find('2')
second2 = s.find('2', first2 + 1)
self.assertEqual(first2, 2)
self.assertEqual(second2, 5)

def test11d(self):
self.assertFalse('Boolean'[0].islower())
self.assertTrue('boolean'[0].islower())

def test11e(self):
self.assertEqual('MoNDaY'.lower().capitalize(), 'Monday')

def test11f(self):
self.assertEqual('  Monday'.lstrip(), 'Monday')

if __name__ == '__main__':
main()
```

```% ./sample9.py -v
test10 (__main__.MyTestCase) ... ok
test11a (__main__.MyTestCase) ... ok
test11bc (__main__.MyTestCase) ... ok
test11d (__main__.MyTestCase) ... ok
test11e (__main__.MyTestCase) ... ok
test11f (__main__.MyTestCase) ... ok
test9 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 7 tests in 0.000s

OK
%```