Python程序是大小写敏感的,另外我们可以为一个变量赋值多个类型的值,在python中通常常量是用大写单词来表示的,但是不保证开发者一定不能修改,所以更多的
时候,算是一种编程约束
python字符串
创建字符串
在python中创建一个字符串,只需要将字符串放在”“,”,”“” “”“当中即可
#声明当前使用的字符集
#coding=utf-8
a = 'hello'
b = "english"
c = """my name is test,\n \"china\""""
print a + "=="+b+"=="+c
此时输出如下:
字符串的下标是从0开始
#和java中是相同的,字符串的下标是从0开始的
a = "hello world"
print a[0] #打印h
print a[:2] #第三个字符之前的所有字符
print a[2:] #第三个字符之后的所有字符
print a[2:5] #第三个字符到第六个字符之间的所有字符
字符串转换成int型或float型
#在python中同样可以实现像java中那样,将字符串转换成int型或float型
a = "12"
b = "3.5"
print int(a) + float(b)
将非法字符串转换成字符串表示
#在pyton中,同样提供,将非法字符串转换成字符串表示:str(),repr(),format(xx,xx)
x = 5.64
print str(x)
print repr(x)
print format(x,"0.1f")
python集合
在python中,集合是无序的,因此不能通过下标来进行索引,并且没有长度限制,用于包含一组无序的对象集合中的元素是不能重复的
使用set()函数创建集合
a = set(["11","123"])
print a #set(['11', '123'])
集合中的元素是不能重复的
b = set(['g','o','o','d'])