100 Amateur Language Processing Knock: 07

It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).

Chapter 1: Preparatory movement

07. Sentence generation by template

Implement a function that takes arguments x, y, z and returns the string "y at x is z". Furthermore, set x = 12, y = "temperature", z = 22.4, and check the execution result.

The finished code:

main.py


# coding: utf-8


def format_string(x, y, z):
	'''Argument x, y,Receives z and returns the string "y at x is z"

argument:
	x, y, z --Parameters to embed
Return value:
Formatted string
	'''
	return '{hour}of time{target}Is{value}'.format(hour=x, target=y, value=z)


#test
x = 12
y = 'temperature'
z = 22.4
print(format_string(x, y, z))

Execution result:

Terminal


The temperature at 12:00 is 22.4

How to specify str.format () is [Format specification string grammar](http://docs .python.jp/3/library/string.html#formatstrings) has an explanation. If you don't use it, it's hard to remember.


Also, there is string.Template class. This may be closer to the intention of the question, so I will write it here as well.

The finished code:

main2.py


# coding: utf-8
from string import Template


def format_string(x, y, z):
	'''Argument x, y,Receives z and returns the string "y at x is z"

argument:
	x, y, z --Parameters to embed
Return value:
Formatted string
	'''
	s = Template('$hour$target is$value')
	return s.substitute(hour=x, target=y, value=z)


#test
x = 12
y = 'temperature'
z = 22.4
print(format_string(x, y, z))

Execution result:

Terminal


The temperature at 12:00 is 22.4

That's all for the 8th knock. If you have any mistakes, I would appreciate it if you could point them out.

Recommended Posts

100 Amateur Language Processing Knock: 07
100 Amateur Language Processing Knock: 09
100 Amateur Language Processing Knock: 47
100 Amateur Language Processing Knock: 97
100 Amateur Language Processing Knock: 67
100 Amateur Language Processing Knock: Summary
100 Language Processing Knock (2020): 28
100 Language Processing Knock (2020): 38
100 language processing knock 00 ~ 02
100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 50
100 language processing knock 2020 [00 ~ 69 answer]
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 Language Processing Knock 2020 Chapter 1
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 language processing knock 2020 [00 ~ 49 answer]
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 Language Processing Knock-52: Stemming
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 04
100 Language Processing Knock Chapter 1
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68