回答編集履歴
2
修正
answer
CHANGED
@@ -8,8 +8,8 @@
|
|
8
8
|
'None'
|
9
9
|
```
|
10
10
|
|
11
|
-
スライスオブジェクトではなく、文字列`:2`が渡ってしま
|
11
|
+
スライスオブジェクトではなく、文字列`:2`が渡ってしまっています。
|
12
|
-
f-stringを使うのが
|
12
|
+
解決するには、Python3.6以降で導入されたf-stringを使うのが良いのではないでしょうか。
|
13
13
|
```Python
|
14
14
|
>>> word = 'highschool'
|
15
15
|
>>> f'{word}は{word[:4]}と{word[4:]}でできています。'
|
@@ -18,8 +18,10 @@
|
|
18
18
|
|
19
19
|
追記
|
20
20
|
---
|
21
|
-
ドキュメントに
|
21
|
+
ドキュメントに次のような記述がありました。
|
22
|
+
> 置換フィールドの文法は以下です:
|
23
|
+
> ```
|
22
|
-
|
24
|
+
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
|
23
25
|
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
|
24
26
|
arg_name ::= [identifier | digit+]
|
25
27
|
attribute_name ::= identifier
|
@@ -27,6 +29,7 @@
|
|
27
29
|
index_string ::= <any source character except "]"> +
|
28
30
|
conversion ::= "r" | "s" | "a"
|
29
31
|
format_spec ::= <described in the next section>
|
32
|
+
> ```
|
30
33
|
|
31
34
|
引用元: [Python 標準ライブラリ » string » 書式指定文字列の文法](https://docs.python.jp/3/library/string.html#format-string-syntax)
|
32
35
|
|
1
追記
answer
CHANGED
@@ -14,4 +14,20 @@
|
|
14
14
|
>>> word = 'highschool'
|
15
15
|
>>> f'{word}は{word[:4]}と{word[4:]}でできています。'
|
16
16
|
'highschoolはhighとschoolでできています。'
|
17
|
-
```
|
17
|
+
```
|
18
|
+
|
19
|
+
追記
|
20
|
+
---
|
21
|
+
ドキュメントには次のような記述がありました。
|
22
|
+
> replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
|
23
|
+
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
|
24
|
+
arg_name ::= [identifier | digit+]
|
25
|
+
attribute_name ::= identifier
|
26
|
+
element_index ::= digit+ | index_string
|
27
|
+
index_string ::= <any source character except "]"> +
|
28
|
+
conversion ::= "r" | "s" | "a"
|
29
|
+
format_spec ::= <described in the next section>
|
30
|
+
|
31
|
+
引用元: [Python 標準ライブラリ » string » 書式指定文字列の文法](https://docs.python.jp/3/library/string.html#format-string-syntax)
|
32
|
+
|
33
|
+
element_indexとしてスライスオブジェクトが認められていないようですね。
|