今天,接着将自己学到的知识记录下来,
首先,书接上文,我们接着讲python基本数组类型
布尔
t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
print(t and f) # Logical AND; prints "False"
print(t or f) # Logical OR; prints "True"
print(not t) # Logical NOT; prints "False"
print(t != f) # Logical XOR; prints "True"
字符串
hello = 'hello' # String literals can use single quotes
world = "world" # or double quotes; it does not matter.
print(hello) # Prints "hello"
print(len(hello)) # String length; prints "5"
hw = hello + ' ' + world # String concatenation
print(hw) # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
print(hw12) # prints "hello world 12"
s = "hello"
print(s.capitalize()) # Capitalize a string; prints "Hello"
print(s.upper()) # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7)) # Right-justify a string, padding with spaces; prints " hello"
print(s.center(7)) # Center a string, padding with spaces; prints " hello "
print(s.replace('l', '(ell)')) # Replace all instances of one substring with another;
# prints "he(ell)(ell)o"
print(' world '.strip()) # Strip leading and trailing whitespace; prints "world"
由此可以看出 python的字符串功能非常强大,有许多函数可以直接操作字符串如
capitize 首字母大写
upper 全大写
rjust 字符串长度不足空格从最左边补
center 字符串长度不足空格两边补
replace 将什么替换成什么
strip 去空格
至此,所有的基本数据格式已经全部结束了,
心得:今天将所有的基本格式都进行了认识,基本和java等语言类似,明天继续了解python的数组