検索

Python Day  String functions

Python は動的に型指定されるプログラミング言語です。つまり、データ型を入力する必要はありません。代わりに、Python はデフォルトでデータ型を受け取ります。
アヒル型プログラミング言語とも呼ばれます。

参考: https://docs.python.org/3/library/string.html
https://peps.python.org/pep-0020/

文字列:

文字列(str) は、一重引用符 ' ' または二重引用符 " " で示される単語を指します。

例:

city = 'Madurai's Jigarthanda is very famous'
print(city)

上記の入力の場合、出力は次のようになります

SyntaxError: unterminated string literal

文字列が ' ' で定義されている場合でも、一重引用符の構文エラーが表示されます。その理由は、文字列が不完全であるためです。Madurai の この単語では ' アポストロフィを使用していますが、Python はこれを文字列と見なし、不完全です.

このエラーは、3 つの連続した ''' ''' または """ """ を使用することで修正できます。

例:

city = '''Madurai's Jigarthanda is very famous'''
print(city)

city2= """Madurai's Jigarthanda is very famous"""
print(city2)

Address = """no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002"""

print(Address)

上記の入力の場合、出力は次のようになります

Madurai's Jigarthanda is very famous
Madurai's Jigarthanda is very famous
no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002

複数行の文字列の場合でも、「"」「"」二重引用符を使用する必要があります。

1.Python ではすべてがオブジェクトです。
2.すべてのオブジェクトには独自のメモリ空間があります。
3.文字列は不変です。
例:

name = 'guru'
degree = 'B.com'
height = 170
sunday = False
print(id(name))
print(id(degree))
print(id(height))
print(id(sunday))

出力は
となります

129009333595248
129009335659968
11759304
10654592

したがって、オブジェクトのメモリを見つけるために print(id(#オブジェクト名)) が使用されます。

インデックス/下付き文字:(வரிசை)
例:
教祖
0123

そのため、個々の文字にアクセスするためにインデックスが使用されます。上記の例では g-0、u-1、r-2、u-3 です。
インデックス作成は常にゼロから始まります。

例:1

name = 'guru'

print(name[0])
print(name[1])
print(name[2])
print(name[3])

出力:

g
u
r
u

例:2

name = 'guru'

print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end=' ')

出力:

g u r u

水平出力の場合は、end=' 'を使用します。end を使用しない場合、出力は例:1 のように垂直になります。

= を割り当てるために使用されます
== は比較に使用されます。

文字列関数:
print(len(#object)) - これは文字列の長さを見つけるために使用されます。

ame = 'guru'

# first letter
print(name[0])
#last letter
print(name[3])
#first letter 'g'
if name[0] == 'g':
    print("yes starts with g")
#last letter 'u'
if name[3] == 'u':
    print("yes ends with u")
#all letters with single space in same line
print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end='\n')


#middle letter
length=len(name)#4
print(name[length//2])

上記の例では、// は床除算と呼ばれる除算に使用されています。小数点の値は取りません。
Atlast n は改行に使用されます。
round() - 小数点以下を四捨五入するために使用されます。

出力:

g
u
yes starts with g
yes ends with u
g u r u
r

いくつかの文字列関数:

capitalize() - 最初の文字を大文字に変換します
casefold() - 文字列を小文字に変換します
openswith() - 文字列が指定された値で終わる場合は true を返します

例:

print(name.capitalize())

name = 'GuruPrasanna'
print(name.casefold())

print(name.endswith('Prasanna'))

出力:

Guru came to class today
guruprasanna
True

以上がPython の日文字列関数の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
リストと配列の選択は、大規模なデータセットを扱うPythonアプリケーションの全体的なパフォーマンスにどのように影響しますか?リストと配列の選択は、大規模なデータセットを扱うPythonアプリケーションの全体的なパフォーマンスにどのように影響しますか?May 03, 2025 am 12:11 AM

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

Pythonのリストと配列にメモリがどのように割り当てられるかを説明します。Pythonのリストと配列にメモリがどのように割り当てられるかを説明します。May 03, 2025 am 12:10 AM

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

Pythonアレイ内の要素のデータ型をどのように指定しますか?Pythonアレイ内の要素のデータ型をどのように指定しますか?May 03, 2025 am 12:06 AM

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

Numpyとは何ですか、そしてなぜPythonの数値コンピューティングにとって重要なのですか?Numpyとは何ですか、そしてなぜPythonの数値コンピューティングにとって重要なのですか?May 03, 2025 am 12:03 AM

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

「隣接するメモリ割り当て」の概念と、配列にとってその重要性について説明します。「隣接するメモリ割り当て」の概念と、配列にとってその重要性について説明します。May 03, 2025 am 12:01 AM

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

Pythonリストをどのようにスライスしますか?Pythonリストをどのようにスライスしますか?May 02, 2025 am 12:14 AM

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

Numpyアレイで実行できる一般的な操作は何ですか?Numpyアレイで実行できる一般的な操作は何ですか?May 02, 2025 am 12:09 AM

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Pythonを使用したデータ分析では、配列はどのように使用されていますか?Pythonを使用したデータ分析では、配列はどのように使用されていますか?May 02, 2025 am 12:09 AM

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。