未分類

python3 で、ある文字列で始まるかどうか判定する方法

投稿日:

文章が、ある文字列で始まるかどうかを判定するには startswith を使う。

str = "こんにちは。"
print(str.startswith('こん'))
print(str.startswith('にち'))

結果

True
False

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python で文字列の長さを表示する方法

len を使う。 例 length = len(‘文字列あいうえお’) print(str(length)) 結果 8

no image

matplotlib で x, y軸・タイトルのフォントサイズを調整する

ラベルの fontsize で指定する。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2, 100) …

no image

python3 で、ファイルの文字列の1行目を出力する

ファイルを読み込んで、1行目だけをコンソールに出力するには readlines() を使う。 例 f = open(“data.txt”) lines = f.readlines() print(li …

no image

tex で数式モード内に普通の文字列を入力する方法

\textrm を使う。 例 $$y = f (x) \hspace{1cm} \textrm{This is a function.}$$ 結果

no image

python3 で、文字列のパーセント(%)をそのまま表示する方法

文字列の始まりに r をつけて、raw 文字列にするとパーセントなどの文字をそのまま表示できる。 例 str1 = r’%s%d\t’ print(str1) 結果 %s%d\t