PHP:
$str = "абвгдежз";
$p = strpos($str, "г");
echo $str, "n", $p;
结果:6
Python:
str = "абвгдежз"
p = str.find("г")
print(str); print(p)
结果:3
PHP的strpos()
不支持多字节,所以它计算的是字节,而不是字符。要在Python中获得等效值,您需要使用.encode()
将字符串转换为字节字符串
str = "абвгдежз"
search = "г"
p = str.encode().find(search.encode())
print(str)
print(p)