成都网站建设设计

将想法与焦点和您一起共享

char函数的用法Python,char函数的用法c

char 是什么意思?

Char 是计算机编程语言(c、c++、java、VFP等)中可容纳单个字符的一种基本数据类型。

井冈山网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

1、char有一个特殊的语言就是char *,它在C/C++中有专门的语义,既不同于signed char *,也不同于unsigned char *,而是用于定义指针变量,如:char *p,这个变量p是个指针变量;就是说,p这个变量里能存储一个char类型的变量的首地址;

2、在MYSQL中,字段类型char是指:使用指定长度的固定长度表示的字符串,如char(8),则数据库会使用固定的1个字节(八位)来存储数据,不足8位的字符串在其后补空字符;

3、在excel中char函数用于返回对应发于数字代码的字符,如=char(number),其中,参数number为转换的字符代码,介于0~255之间,char占一个字节,也就是8个二进制位,但它表示的是有符号的类型,所以表示的范围是-128~127,uchar表示无符号的类型,所以表示的范围是0~255。

python调用c函数

Python是解释性语言, 底层就是用c实现的, 所以用python调用C是很容易的, 下面就总结一下各种调用的方法, 给出例子, 所有例子都在ubuntu9.10, python2.6下试过

1. Python 调用 C (base)

想在python中调用c函数, 如这儿的fact

#include Python.h

int fact(int n)

{

if (n = 1)

return 1;

else

return n * fact(n - 1);

}

PyObject* wrap_fact(PyObject* self, PyObject* args)

{

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", n))

return NULL;

result = fact(n);

return Py_BuildValue("i", result);

}

static PyMethodDef exampleMethods[] =

{

{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},

{NULL, NULL}

};

void initexample()

{

PyObject* m;

m = Py_InitModule("example", exampleMethods);

}

把这段代码存为wrapper.c, 编成so库,

gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so库的目录, 进入python, 可以如下使用

import example

example.fact(4)

2. Python 调用 C++ (base)

在python中调用C++类成员函数, 如下调用TestFact类中的fact函数,

#include Python.h

class TestFact{

public:

TestFact(){};

~TestFact(){};

int fact(int n);

};

int TestFact::fact(int n)

{

if (n = 1)

return 1;

else

return n * (n - 1);

}

int fact(int n)

{

TestFact t;

return t.fact(n);

}

PyObject* wrap_fact(PyObject* self, PyObject* args)

{

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", n))

return NULL;

result = fact(n);

return Py_BuildValue("i", result);

}

static PyMethodDef exampleMethods[] =

{

{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},

{NULL, NULL}

};

extern "C" //不加会导致找不到initexample

void initexample()

{

PyObject* m;

m = Py_InitModule("example", exampleMethods);

}

把这段代码存为wrapper.cpp, 编成so库,

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so库的目录, 进入python, 可以如下使用

import example

example.fact(4)

3. Python 调用 C++ (Boost.Python)

Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.

首先在ubuntu下安装boost.python, apt-get install libboost-python-dev

#include boost/python.hpp

char const* greet()

{

return "hello, world";

}

BOOST_PYTHON_MODULE(hello)

{

using namespace boost::python;

def("greet", greet);

}

把代码存为hello.cpp, 编译成so库

g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1

此处python路径设为你的python路径, 并且必须加-lboost_python-gcc42-mt-1_34_1, 这个库名不一定是这个, 去/user/lib查

然后在有此so库的目录, 进入python, 可以如下使用

import hello

hello.greet()

'hello, world'

4. python 调用 c++ (ctypes)

ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.

ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.

#include Python.h

class TestFact{

public:

TestFact(){};

~TestFact(){};

int fact(int n);

};

int TestFact::fact(int n)

{

if (n = 1)

return 1;

else

return n * (n - 1);

}

extern "C"

int fact(int n)

{

TestFact t;

return t.fact(n);

}

将代码存为wrapper.cpp不用写python接口封装, 直接编译成so库,

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

进入python, 可以如下使用

import ctypes

pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')

pdll.fact(4)

12

一个python函数的问题

调用str2int函数,会首先定义这两个子函数

然后返回 reduce(fn,map(char2num,s))

也就是首先对s中每个元素执行char2num函数,返回值组成map中的元素,再对map中的元素累计执行fn函数,然后返回结果

如str2(’123’) 以'1','2','3'为参数调用 char2num先返回数字 1,2,3,然后对1,2,3累计执行fn(x,y)

1*10+2=12

12*10+3=123

最后 返回数字123

python之字符串内置函数

1. 字符串字母处理

2. 字符串填充

str.ljust(width, fillchar)、str.center(width, fillchar)、str.rjust(width, fillchar)

返回一个指定的宽度 width 「居左」/「居中」/「居右」的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

3,字符串计数

str.count(sub, start, end)

#统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

start, end遵循**“左闭右开”**原则。

4. 字符串位置

str.endswith(suffix, start, end)和str.startswith(substr, beg, end)

#判断字符串是否以指定后缀结尾/开头,如果以指定后缀「结尾」/「开头」返回 True,否则返回 False。

5. 字符串查找

6. 字符串判断

7. 字符串拼接

str.join() #将序列中的元素以指定的字符连接生成一个新的字符串。

s1 = "-" s2 = "" seq = ("r", "u", "n", "o", "o", "b")

# 字符串序列 print (s1.join( seq )) print (s2.join( seq )) r-u-n-o-o-b runoob

8. 统计字符串长度

str.len() #返回对象(字符、列表、元组等)长度或项目个数。

9. 去除字符两侧空格

str.lstrip()、str.rstrip()、str.strip() #截掉字符串「左边」/「右边」/「左右」两侧的空格或指定字符。

str0 = ' Hello World!' str0.lstrip() 'Hello World!' str1 = 'aaaa Hello World!' str1.lstrip('a') ' Hello World!'

10. str.maketrans(intab, outtab)和str.translate(table)

str.maketrans()创建字符映射的转换表

str.maketrans()根据参数table给出的表转换字符串的字符。

str.maketrans()传入的也可以是字典

tab = {'e': '3', 'o': '4'} trantab = str.maketrans(tab) str0.translate(trantab) 'H3ll4 W4rld!'

11. 字符串替换

str.replace(old, new, max)

12. 字符分割

str.split(str, num)

13. 字符填充

str.zfill(width)

返回指定长度的字符串,原字符串右对齐,前面填充0。

python中char是什么意思

至少目前python中是没有char这个关键字的。

如果你在某段代码中看到char,说明你看到的可能是一个变量名、函数名、类名,再或者是个字符串。


网站栏目:char函数的用法Python,char函数的用法c
URL标题:http://chengdu.cdxwcx.cn/article/dsiisjd.html