Python 文字列の1文字取得するサンプル

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

構文
文字列[ 位置 ]
「位置」にある文字を返します。
最初の1文字目の位置は0です。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
str = 'テスト太郎'
print(str[0])
print(str[1])
print(str[-1])
print(str[-2])
# coding: utf-8 str = 'テスト太郎' print(str[0]) print(str[1]) print(str[-1]) print(str[-2])
# coding: utf-8

str = 'テスト太郎'

print(str[0])
print(str[1]) 
print(str[-1])
print(str[-2])

実行結果



Python

Posted by arkgame