投稿日: | 最終更新日:
pythonの文字列とプレースホルダー
文字列
文字列を変数に代入
>>> fred = silly_string = 'Why do gorillas have big nostrils? Big fingers!!'
文字列を変数に代入するには、文字列の最初と最後をシングルクオーテーションで囲みます。
>>> fred = silly_string = "Why do gorillas have big nostrils? Big fingers!!"
ダブルクオーテーションで囲んでも正しく代入できます。
複数行の文字列を変数に代入
>>> fred = silly_string = 'How do dinosaurs pay their bills? SyntaxError: EOL while scanning string literal
silly_string = ‘How do dinosaurs pay their bills?
クオート文字で文字列を終了させないままEnterキーをクリックします。
すると、「SyntaxError: EOL while scanning string literal」エラーが表示されます。
silly_string = ”’How do dinosaurs pay their bills?
With tyrannosaurus checks!”’
複数行の文字列を作りたい場合は、クオート文字を三つ連続して書きます。
文字列の中にシングルクオーテーションやダブルクオーテーションを含む場合
>>> silly_string = silly_string = 'He said, "Aren't can't shouldn't wouldn't"' SyntaxError: invalid syntax
‘He said, “Aren’t can’t shouldn’t wouldn’t”‘
上記の文字列は、最初と最後をシングルクオーテーションで正しく囲んでます。
しかし、文字列の中にシングルクオーテーションを含んでいますので「SyntaxError」が出ます。
>>> silly_string = '''He said, "Aren't can't shouldn't wouldn't"'''
”’He said, “Aren’t can’t shouldn’t wouldn’t””’
文字列を正しく変数に代入するには、最初と最後をシングルクオーテーション3つで囲みます。
>>> silly_string = 'He said, "Aren\'t can\'t shouldn\'t wouldn\'t"'
‘He said, “Aren\’t can\’t shouldn\’t wouldn\’t”‘
バックスラッシュ文字をクオート文字の前に記述する方法もあります。
これを「エスケープ」と呼びます。
プレースホルダーでデータを文字列に埋め込む
>>> myscore = 100 >>> message = 'I scored %s points' >>> print(message % myscore) I scored 100 points
変数に代入されたデータをメッセージに使います。
文字列に変数のデータを埋め込むには、文字列中に「%s」を記述します。
「%」を使った埋め込み場所を示すマークをプレースホルダーと呼びます。
複数の変数を埋め込む場合
>>> joke_text = '%s: a device for finding furniture in the dark' >>> bodypart1 = 'knee' >>> bodypart2 = 'shin' >>> print(joke_text % bodypart1) knee: a device for finding furniture in the dark >>> print(joke_text % bodypart2) shin: a device for finding furniture in the dark
違う内容の2つの変数を使うことで、2つの異なる結果が得られます。
複数プレースホルダーがあった場合
>>> nums = 'what did the number %s say to number %s? Nice belt!!' >>> print(nums % (0, 8)) what did the number 0 say to number 8? Nice belt!!
複数のプレースホルダーを使うときは、置き換えるデータ「0, 8」をカッコで囲みます。
先頭の「0」から順番にプレースホルダーに埋め込まれます。
文字列の掛け算
>>> print('a' * 10 ) aaaaaaaaaa
文字列に掛け算をすることも可能です。’a’に10をかけると、’aaaaaaaaa’になります。
- Python 114
- 制作 54
- RaspberryPi 41
- Django 40
- WordPress 40
- Linux 27
- VPS 22
- JavaScript 21
- PHP 20
- HTML・CSS 19
- AWS 16
- 仮想環境 15
- レスポンシブデザイン 13
- マイコン 11
- WEB全般 11
- 動画製作 9
- Webサービス 8
- 統合開発環境 8
- 機械学習 8
- PyCharm 7
- jQuery 7
- AfterEffects 7
- 起業・設立 7
- Django REST framework 6
- C# 6
- デザイン 6
- SEO 6
- pydata 6
- Visual Studio 5
- 数学 5
- 携帯サイト 5
- heroku 5
- Mac 5
- illustrator 5
- node.js 5
- Anaconda 5
- Nginx 4
- Jupyter Notebook 4
- インフラ 4
- Google Colaboratory 4
- symfony 4
- Webスクレイピング 3
- photoshop 3
- Go言語 3
- PC 3
- ツール 3
- Docker 3
- facebook 3
- 作業効率化 3
- データベース 3
- Cloud9 3
- コマンド 2
- micro:bit 2
- Kali Linux 2
- Webサーバー 2
- MariaDB 2
- ドローン 2
- コンテナ 2
- DaVinci Resolve 2
- ネットワーク 2
- Java 2
- movie 2
- PCDJ 2
- 音楽 2
- XSERVER 2
- Ansible 1
- Vue.js 1
- JSON 1
- Bootstrap 1
- バージョン管理システム 1
- SSL 1
- S3 1
- ムームードメイン 1
- ネットワーク 1
- アニメーション 1
- D3.js 1
- Rhino 1
- アニメ 1
- git 1
- windows 1
- アクセス解析 1
- スマートフォン 1
- アフィリエイトノウハウ 1
- 知識 1
- TypeScript 1
- 役立つ本・書籍 1
- データサイエンス 1
- ESP32 1
- AI 1
- ownCloud 1
- API 1