zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python中camel函数的使用

2023-03-20 14:45:01 时间

python中camel函数的使用

1、camel接收字符串形式的变量名,并将其转换为驼峰形式。

2、这个函数考虑的是变量形式的字符串,单词之间有相关的分隔,而不是直接连续的单词。

比如somefunctionname。

实例

from re import sub
 
def camel(s):
  s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
  return s[0].lower() + s[1:]
 
# EXAMPLES
camel('some_database_field_name') # 'someDatabaseFieldName'
camel('Some label that needs to be camelized') # 'someLabelThatNeedsToBeCamelized'
camel('some-javascript-property') # 'someJavascriptProperty'
camel('some-mixed_string with spaces_underscores-and-hyphens') # 'someMixedStringWithSpacesUnderscoresAndHyphens'

以上就是python中camel函数的使用,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。