s = 'ABCDEF'
#索引
#s1,s2,s3...都是新字符串与s无关了
s1 = s[0]
s2 = s[-1] #最后一个元素
s3 = s[0:4] #左闭右开
print(s1) #A
print(s2) #F
print(s3) #ABC
#打印全部
s4 = s[:] #s[0:]
print(s4)
s5 = s[0:5:2] #[首:位:步长] 步长为正,正着取,步长为负,倒着取
print(s5) #ACE
s6 = s[3::-1]
print(s6) #DCBA
#倒置
s7 = s[::-1]
print(s7) #FEDCBA
s = 'aaaaBBBB'
#首字母大写
s1 = s.capitalize()
print(s1)
s2 = s.upper() #全部大写
s21 = s.lower() #全部小写
print(s2,s21)
s3 = s.swapcase()#反转大小写
print(s3)
#每个首字母大写
s4 = 'abc asc asd'#s4 = 'abc*asc_asd' 用空格,特殊字符,数字隔开计算字符串个数
s5 = s4.title()
print(s5)
b.大小写变换
s = 'aaaaBBBB'
#首字母大写
s1 = s.capitalize()
print(s1)
s2 = s.upper() #全部大写
s21 = s.lower() #全部小写
print(s2,s21)
s3 = s.swapcase()#反转大小写
print(s3)
#每个首字母大写
s4 = 'abc asc asd'#s4 = 'abc*asc_asd' 用空格,特殊字符,数字隔开计算字符串个数
s5 = s4.title()
print(s5)
c.居中
#居中
s6 = s.center(20,'_') #居中,然后前后用_填充
print(s6)
d.查找
#startswith 字符串以什么为开头
#endswith 字符串以什么为结尾
s7 = s.startswith('a',0,3) #判断s[0]-s[2]首字母是否为a,真返回TRUE,假返回FALSE
print(s7)
#find 通过元素找索引,找不打返回-1
#index 通过元素找索引,找不到报错
s8 = s.find('a')
print(s8,type(s8)) #返回的是下标,int类型
s8 = s.index('a')
print(s8,type(s8))
e.删除左右空格
#strip 默认删除空格 rstrip lstrip
s = '*alll%'
s1 = s.strip('%*') #括号里不分先后
print(s1)
#alll
f.计数
s = 'aaalllss哈哈'
s1 = len(s)
s2 = s.count('l') #count可以切片
print(s1) #汉字也算一个元素
print(s2)
g.替换
replace(old,new,count)#count替换次数
h.把一个字符串分割成列表
s = 'b a c'
s3 = s.split() #默认为空格,所有空格都删掉
print(s3)
#['a','b','c']
i.把字符串里的\t转化为tab
s = 'aa\tbb'
s1 = s.expandtabs()
print(s1) #aa后面补充6个空格
#aa bb
j.format格式化输出
a = '姓名:{},年龄:{},我是{}'.format('铁头',18,'铁头')
b = '姓名:{0},年龄:{1},我是{0}'.format('铁头',18)
c = '姓名:{name},年龄:{age},我是{name}'.format(age = 18,name = '铁头')
print(a)
print(b)
print(c)
'''
姓名:铁头,年龄:18,我是铁头
姓名:铁头,年龄:18,我是铁头
姓名:铁头,年龄:18,我是铁头
'''
1.3 int
i = 3 #转化为二进制所占最少位数
print(i.bit_length())
1.4 bool
#int --->str
i = 1
s = str(i)
#str--->int 字符串必须全是数字,否则报错如'123a',有空格也可以
s = '123'
i = int(s)
#int --->bool
i = 3
b = bool(i)
#str--->bool 非空就是TRUE
#s = ''--- >False
转化成bool值为False的数据类型有:
---------------------
作者:铁头同学
来源:CSDN
原文:https://blog.csdn.net/zeroooorez/article/details/89192278
版权声明:本文为博主原创文章,转载请附上博文链接!class Person(object):
def run(self):
print("run")
def eat(self,food):
print("eat " + food)
def say(self):
print("My name is %s,I am %d years old" % (self.name,self.age))
# 构造函数,创建对象时默认的初始化
def __init__(self,name,age,height,weight,money):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.__money = money #实际上是_Person__money
print("哈喽!我是%s,我今年%d岁了。目前存款%f" %(self.name,self.age,self.__money))
# 想要内部属性不被直接外部访问,属性前加__,就变成了私有属性private
self.__money = 100
# 私有属性需要定义get、set方法来访问和赋值
def setMoney(self,money):
if(money < 0):
self.__money = 0
else:
self.__money = money
def getMoney(self):
return self.__money
person = Person("小明", 5, 120, 28,93.1)
# 属性可直接被访问
person.age = 10
print(person.age)
# 私有属性不可直接被访问或赋值,因为解释器把__money变成了_Person__money(可以用这个访问到私有属性的money,但是强烈建议不要),以下2行会报错
# person.money = 10
# print(person.__money)
# 可以调用内部方法访问和赋值
print(person.getMoney())
person.setMoney(-10)
print(person.getMoney())
import java.util.Scanner;
import java.util.Stack;
class Person{
static {
System.out.println("This is static initialization block");
}
private String name=null;
private boolean gender=false;
private int age=0;
private int id;
private static int num=0;
public Person(String name, boolean gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
id=num;
num++;
}
public Person() {
System.out.println("This is initialization block, id is "+num);
System.out.println("This is constructor");
id=num;
num++;
System.out.println(name+","+age+","+gender+","+id);
System.out.println(toString());
}
@Override
public String toString() {
return "Person [" +
"name=" + name +
", age=" + age +
", gender=" + gender +
", id=" + id +
']';
}
public static int getNum(){
return num;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int time=sc.nextInt();
Stack stack=new Stack();
for (int i=0;i<time;i++){
String name=sc.next();
int age=Integer.valueOf(sc.next());
boolean gender=Boolean.valueOf(sc.next());
Person p=new Person(name,gender,age);
System.out.println("This is initialization block, id is "+i);
stack.push(p);
}
for (int i=0;i<Person.getNum();i++){
Person person=(Person) stack.peek();
System.out.println(person);
stack.pop();
}
new Person();
}
}
2.当 extern 修饰变量时
正确使用方法是:在 .c 文件中定义变量,在相应的 .h 文件中进行声明。
我们通过是否会为变量来分配内存空间来判定是声明还是定义(严格来说,是单纯的分配内存,并不包括初始化部分)。那么 int i; 这句话是声明还是定义那?它既是声明,也是定义。如果我们在 test.h 文件中使用这句话,一旦在其他文件中定义 i(e.g.1),或者该文件被重复包含(e.g.2),那么就会产生重定义的错误。
/*
e.g.1 以下为3个文件
*/
//test.h
int i;
//test2.h
int i;
//main.cpp
#include "test.h"
#include "test2.h"
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
e.g.2 以下为3个文件
*/
//test.h
int i;
//test2.h
#include "test.h"
//main.cpp
#include "test.h"
#include "test2.h"
int main()
{
return 0;
}
---------------------