0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのprint文を使ってためしてみた。(10/30)

Last updated at Posted at 2024-10-01

今回はPythonの基本中の基、print文について30通りの使い方を紹介します。
初心者の方でも理解しやすいようシンプルに紹介します。
また、金融、IT企業、スタートアップに興味をもつきっかけとして
関連する用語を使用しています。

この記事から得られること

  • print文の使い方になれることができる。
  • プログラム開発中の変数等の中身を確認できることが理解できる。
  • print文の使い方のアイディアのストックが増える。

なぜこの記事を書こうと思ったのか。

その理由は、プログラムの開発中に意外にも役立っていたので、
シェアしたいと思ったから。
また、使える種類が増えると、変数、リストの値など、確認しやすいと思ったから。

1. 基本の文字列出力

python_1.py
print("Fintech is the future")

実行結果:

Fintech is the future

直接ダブルクォーテーション("")の中に文字列を入れるパターン。
シングルクォーテーション('')でも可能です。

2. 複数の文字列を連結

python_2.py
print("Blockchain" + " " + "technology")

文字列  + " "(半角空白) + 文字列 の形

実行結果:

Blockchain technology

3. カンマで区切って複数の要素を出力

python_3.py
print("Stocks", "Bonds", "Commodities")

各要素が空白区切りで表示されます。

実行結果:

Stocks Bonds Commodities

4. フォーマット文字列(f-string)の使用

python_4.py
company = "TechStartup"
print(f"{company} is disrupting the industry")

{company}の部分が変数companyに格納されているTechStartupに置き換えられて表示されます。
実行結果:

TechStartup is disrupting the industry

5. 改行を含む文字列

python_5.py
print("IPO\nM&A\nVenture Capital")

\nが改行記号です。

実行結果:

IPO
M&A
Venture Capital

6. エスケープ文字を使用

python_6.py
print("\"Unicorn\" status achieved")

プログラムの文法としての動作ではなく、文字列に使われる"として動作します。
実行結果:

"Unicorn" status achieved

7. リストの内容を出力

python_7.py
investments = ["Angel", "Seed", "Series A"]
print(investments)

リスト内の要素が表示されます。
実行結果:

['Angel', 'Seed', 'Series A']

ほしい要素を取得したい場合、要素番号を指定します。
0番目の要素を取得したい場合

python_7_1.py
investments = ["Angel", "Seed", "Series A"]
print(investments[0])

0番目の要素の「Angel」が表示されます。

実行結果:

Angel

8. タプルの内容を出力

python_8.py
quarterly_results = ("Revenue growth", "Profit margin", "User acquisition")
print(quarterly_results)

実行結果:

('Revenue growth', 'Profit margin', 'User acquisition')

タプルはそのままだと、要素の変更ができません。
しかし、リスト型に変更が可能です。

python_8_1.py
quarterly_results = ("Revenue growth", "Profit margin", "User acquisition")
a, b, c = quarterly_results
quarterly_results_list = [a, b, c]
print(quarterly_results_list)

実行結果:

['Revenue growth', 'Profit margin', 'User acquisition']

9. 区切り文字を指定して出力

result_9.py
print("AI", "ML", "Deep Learning", sep=" | ")

3.で空白区切りで表示されていたのが、sep=""""の間に指定された文字列に置き換わります。
実行結果:

AI | ML | Deep Learning

10. 末尾の文字を指定して出力

result_10.py
print("Funding round", end=": ")
print("Series B")

実行結果:

Funding round: Series B

続きは次回の記事になります。
ここまで、何か学びがあれば、今すぐ使ってみてください。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?