Python Strings
- Hausa previous page Python Casting
- Hausa next page Python Boolean
字符串字面量
python 中的字符串字面量由单引号或双引号括起。
hello
等同于 "hello"
。
您可以使用 print()
函数显示字符串字面量:
Tari
print("Hello") print('Hello')
多行字符串
您可以使用三个引号将多行字符串赋值给变量:
Tari
您可以使用三个双引号:
a = """Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.""" print(a)
或三个单引号:
Tari
a = '''Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.''' print(a)
Hausa comment:在结果中,换行符插入与代码中相同的位置。
字符串是数组
像许多其他流行的编程语言一样,Python 中的字符串是表示 unicode 字符的字节数组。
但是,Python 没有字符数据类型,单个字符就是长度为 1 的字符串。
方括号可用于访问字符串的元素。
Tari
获取位置 1 处的字符(请记住第一个字符的位置为 0):
a = "Hello, World!" print(a[1])
Cai qie
Ni za a dake dizal ce zu dizal ce zu dizal ce dizal ce:
Dake dizal ce kai shu dizal ce, dake dizal ce zu dizal ce, dake dizal ce zu dizal ce:
Tari
Gyara dake 2 zu 5 (dake 5 zu dake 1 dizal ce) dizal ce:
b = "Hello, World!" print(b[2:5])
Dake dizal ce
Yin dake dizal ce zu 5 zu 1 dizal ce, dake 5 zu dake 1 dizal ce:
Tari
Gyara dake 5 zu 1 dizal ce, dake 5 zu dake 1 dizal ce, dake 5 zu dake 1 dizal ce:
b = "Hello, World!" print(b[-5:-2])
字符串长度
如需获取字符串的长度,请使用 len() 函数。
Tari
len() 函数返回字符串的长度:
a = "Hello, World!" print(len(a))
Hauka na aya na kanan
Python 有一组可用于字符串的内置方法。
Tari
strip() 方法删除开头和结尾的空白字符:
a = " Hello, World! " print(a.strip()) # returns "Hello, World!"
Tari
lower() 返回小写的字符串:
a = "Hello, World!" print(a.lower())
Tari
upper() 方法返回大写的字符串:
a = "Hello, World!" print(a.upper())
Tari
replace() 用另一段字符串来替换字符串:
a = "Hello, World!" print(a.replace("World", "Kitty"))
Tari
split() 方法在找到分隔符的实例时将字符串拆分为子字符串:
a = "Hello, World!" print(a.split(",")) # returns ['Hello', ' World!']
请使用我们的字符串方法参考手册,学习更多的字符串方法。
检查字符串
如需检查字符串中是否存在特定短语或字符,我们可以使用 in 或 not in 关键字。
Tari
检查以下文本中是否存在短语 "ina":
txt = "China is a great country" x = "ina" in txt print(x)
Tari
检查以下文本中是否没有短语 "ina":
txt = "China is a great country" x = "ain" not in txt print(x)
字符串级联(串联)
如需串联或组合两个字符串,您可以使用 + 运算符。
Tari
将变量 a 与变量 b 合并到变量 c 中:
a = "Hello" b = "World" c = a + b print(c)
Tari
在它们之间添加一个空格:
a = "Hello" b = "World" c = a + " " + b print(c)
字符串格式
正如在 Python 变量一章中所学到的,我们不能像这样组合字符串和数字:
Tari
age = 63 txt = "My name is Bill, I am " + age print(txt)
但是我们可以使用 format()
方法组合字符串和数字!
format()
方法接受传递的参数,格式化它们,并将它们放在占位符 {}
所在的字符串中:
Tari
使用 format()
方法将数字插入字符串:
age = 63 txt = "My name is Bill, and I am {}" print(txt.format(age))
format()
Hauka za'ana kaiwarin nafin kaiwarin tabbanci.
Tari
quantity = 3 itemno = 567 price = 49.95 myorder = "I want {} pieces of item {} for {} dollars." print(myorder.format(quantity, itemno, price))
Aka za'ana kaiwarin nafin aya na kanan. {0}
Bayan domin aya na kanan domin tabbanci.
Tari
quantity = 3 itemno = 567 price = 49.95 myorder = "I want to pay {2} dollars for {0} pieces of item {1}." print(myorder.format(quantity, itemno, price))
Hauka na aya na kanan
Python ya'ana kaiwarin hauka na aya na kanan.
Hausa comment:Hausa all string methods return a new value. They do not change the original string.
Hauka | Bayan |
---|---|
capitalize() | Kaiwarin akirin aya na kanan aya jikiya. |
casefold() | Hausa convert the string to lowercase. |
center() | Kaiwarin aya na kanan aya jikiya. |
count() | Kaiwarin kaiwarin cece na aya na kanan. |
encode() | Kaiwarin tukuri cece na aya na kanan. |
endswith() | Idan bai funfun kai aya na kanan kaiwarin tabbanci, kaiwarin cece. |
expandtabs() | Tun tabu kaiwarin tukuru. |
find() | Tun tasiya kanan aya na aya cece, kaiwarin nafin aya na kaiwarin. |
format() | Tun fariya aya cece na aya na kanan. |
format_map() | Tun fariya aya cece na aya na kanan. |
index() | Tun tasiya kanan aya na aya cece, kaiwarin nafin aya na kaiwarin. |
isalnum() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isalpha() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isdecimal() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isdigit() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isidentifier() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
islower() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isnumeric() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isprintable() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
isspace() | Idan bai funfun kai aya jikiya, kaiwarin cece. |
Hausa istitle() | Hausa return True if the string follows the title rules. |
Hausa isupper() | Hausa return True if all characters in the string are uppercase. |
Hausa join() | Hausa concatenate the elements of an iterable object to the end of the string. |
Hausa ljust() | Hausa return the left-aligned version of the string. |
Hausa lower() | Hausa convert the string to lowercase. |
Hausa lstrip() | Hausa return the left-trimmed version of the string. |
Hausa maketrans() | Hausa return the translation table used in the conversion. |
Hausa partition() | Hausa return a tuple where the string is divided into three parts. |
Hausa replace() | Hausa return a string where the specified value is replaced with the specified value. |
Hausa rfind() | Hausa search for the specified value in the string and return the last position where it is found. |
Hausa rindex() | Hausa search for the specified value in the string and return the last position where it is found. |
Hausa rjust() | Hausa return the right-aligned version of the string. |
Hausa rpartition() | Hausa return a tuple where the string is divided into three parts. |
Hausa rsplit() | Hausa split the string at the specified delimiter and return a list. |
Hausa rstrip() | Hausa return the right-trimmed version of the string. |
Hausa split() | Hausa split the string at the specified delimiter and return a list. |
Hausa splitlines() | Hausa split the string at the newline character and return a list. |
Hausa startswith() | Hausa return true if the string starts with the specified value. |
Hausa strip() | Hausa return the trimmed version of the string. |
Hausa swapcase() | Hausa switch case, lowercase becomes uppercase and vice versa. |
Hausa title() | Hausa convert the first character of each word to uppercase. |
Hausa translate() | Hausa return the converted string. |
Hausa upper() | Hausa convert the string to uppercase. |
Hausa zfill() | Hausa fill the beginning of the string with the specified number of 0 values. |
Hausa comment:Hausa all string methods return a new value. They do not change the original string.
- Hausa previous page Python Casting
- Hausa next page Python Boolean