LoginSignup
0
0

More than 3 years have passed since last update.

Pythonの文字出力(Ruby学習後のPython初学)

Last updated at Posted at 2020-12-03

Pythonの文字出力(Rubyとの比較もほんの少し)

text出力

print("hello world")

hello world がtext出力される。

'''
比較# Rubyの場合は print "hello world"
違い# ( )で閉じない
'''

HTML出力

print("< h1 >hello world< /h1 >")

で< h1 >タグで囲まれた hello world の見出しが出力される。 #タグ内スペースは通常不要

参照# < h1 >や< p >タグなどの段落をもつタグは閉じタグまで1行で出力されるので、

print("< p >hello")
print("world< /p >")

と書いてもHTML上では1行となる。
注意# ただし、段落を持つタグで閉じられていない場合はprintごとに改行される。

他の記述の仕方として

print('''< p >hello
world< /p >''')

print("< p >"hello","world"< /p >)

と書き換えられる。

今回のRubyとの比較

・( )で囲むのが違う。
・ダブルクォーテーションとシングルクォーテーションの文字列に対しての使い方は変化ないが、
シングルクォーテーション三つで囲むのはPython特有だと感じた。

変数

変数の定義はRubyと同様

string = "1"
integer = 1

などで定義できる。

さらに

int += 1 などの演算子も同様に扱える。

文字列の場合は、print(str + str) で 1 1 など出力、
数値の場合は、print(int + int) で 2 など出力できる。

ただし文字列と数値は同時に計算は出来ないので
str + int は出来ないので(エラーとなる)
str(integer)にして、 "1" と文字列に変化させたり、
int(string)にして、 1 と数値に変化させることが必要。

0
0
3

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