Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonでの三重引用符


Pythonの三重引用符は、文字列が逐語的なNEWLINE、TAB、その他の特殊文字を含む複数の行にまたがることを可能にすることで救いの手を差し伸べます。

三重引用符の構文は、3つの連続する一重引用符または二重引用符で構成されます。

#!/usr/bin/python
para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up.
"""
print para_str

出力

上記のコードを実行すると、次のような結果になります。

すべての特殊文字が、「上」の間の文字列の最後にあるNEWLINE最後まで印刷された形式に変換されていることに注意してください。トリプルクォートを閉じます。また、NEWLINEは、行末の明示的なキャリッジリターンまたはそのエスケープコード(\ n)-

のいずれかで発生することに注意してください。
this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up.

生の文字列は、バックスラッシュを特殊文字としてまったく扱いません。生の文字列に入れるすべての文字は、書いたままです-

#!/usr/bin/python
print 'C:\\nowhere'

出力

上記のコードを実行すると、次の結果が生成されます-

C:\nowhere

それでは、生の文字列を利用しましょう。 r'expression'に式を入れます 次のように-

#!/usr/bin/python
print r'C:\\nowhere'

出力

上記のコードを実行すると、次の結果が生成されます-

C:\\nowhere

  1. Pythonで文字列を逆にする

    文字の配列があるとします。追加のスペースを使用せずに文字列を反転する必要があります。したがって、文字列が[H、E、L、L、O]のような場合、出力は[O、L、L、E、 H] これを解決するには、次の手順に従います- 2つのポインタを使用して、開始=0および終了=文字列の長さ– 1 最初と最後の文字を入れ替える 開始を1増やし、終了を1減らします 例 理解を深めるために、次の実装を見てみましょう- class Solution(object):    def reverseString(self, s):       "&q

  2. Pythonのcasefold()文字列

    この関数は、単語の文字を小文字に変換するのに役立ちます。 2つの文字列に適用すると、文字の大文字小文字の種類に関係なく、それらの値と一致する可能性があります。 casefold()の適用 以下の例では、casefold()関数を文字列に適用すると、結果はすべて小文字で出力されます。 例 string = "BestTutorials" # print lowercase string print(" lowercase string: ", string.casefold()) 出力 上記のコードを実行すると、次の結果が得られます- Lowerca