在Python中的驼峰命名

在Python中的驼峰命名

驼峰命名的含义是什么

驼峰命名是一种命名规范,用于给包含多个单词的文件或属性命名,所有单词的首字母都大写。驼峰命名是一种编程语言,可以让您在不违反底层语言命名规则的情况下命名文件或函数。

驼峰命名得名于其外观,看起来像是骆驼的背部。许多不允许文件名中使用空格的编程语言都使用它。驼峰命名允许开发人员为其创建更具特色和有意义的标题。

例如,HelloWorld、Helloworld和helloWorld比helloworld更易于阅读。

将字符串转换为驼峰命名的方法

原生的方法

解决方法的步骤如下。首先创建一个空字符串。然后将字符串中每个单词的首字母大写,其余字母小写,然后将单词连接起来。最后通过将首字母小写来返回最终的字符串。

代码

def camelcase(list_words):
    converted = "".join(word[0].upper() + word[1:].lower() for word in list_words)
    return converted[0].lower() + converted[1:]

words = ["Hello", "Welcome", "To", "Python", "Programming", "In", "Javatpoint"]
print(camelcase(words))

输出:

helloWelcomeToPythonProgrammingInJavatpoint

使用re模块

在这个示例中,我们将使用re模块的sub函数

代码

# importing the module
from re import sub

# creating a function which will convert string to camelcase
def convert_to_camelCase(my_string):
    my_string = sub(r"(_|-)+", " ", my_string).title().replace(" ", "")
    return my_string[0].lower() + my_string[1:]

# taking example strings 
s1 = "Python javatpoint"
s2 = "Python,javatpoint"
s3 = "Python_javatpoint"
s4 = "python_javatpoint.tutorial-camelcase"

print("original s1: ", s1)
print("camelCase of s1: ", convert_to_camelCase(s1))
print()

print("original s2: ", s2)
print("camelCase of s2: ", convert_to_camelCase(s2))
print()

print("original s3: ", s3)
print("camelCase of s3: ", convert_to_camelCase(s3))
print()

print("original s4: ", s4)
print("camelCase of s3: ", convert_to_camelCase(s4))
print()

输出:

original s1:  Python javatpoint
camelCase of s1:  pythonJavatpoint

original s2:  Python,javatpoint
camelCase of s2:  python,Javatpoint

original s3:  Python_javatpoint
camelCase of s3:  pythonJavatpoint

original s4:  python_javatpoint.tutorial-camelcase
camelCase of s3:  pythonJavatpoint.TutorialCamelcase

通过使用split(),join(),title() 和 Generator 表达式

可以通过混合使用上述函数来解决此问题。我们首先将所有下划线分隔;接下来,我们将第一个单词附加到最终字符串中。我们将使用生成器表达式即 a for 循环和 title() 函数进行处理标题大小写的单词。

代码

# This code will use split(), join(), title() and a generator expression to convert a given string to camelcase

# creating a string
string = 'javatpoint_is_best_for_coding_tutorials'

# printing initial string
print("The original string is : " + string)

# ssplitting string from underscores
split = string.split('_')

# joining each part by capitalizing first letter 
camelcase = split[0] + ''.join(word.title() for word in split[1:])

# printing result 
print("The camelcase of the string created is : " + camelcase)

输出:

The original string is : javatpoint_is_best_for_coding_tutorials
The camelcase of the string created is : javatpointIsBestForCodingTutorials

使用split(),join(),title()和map()

可以通过使用上述函数的组合来解决这个问题。使用map()函数,我们可以对完整字符串应用逻辑。

代码

# This code will use split(), join(), title() and map() to convert a given string to camelcase

# creating a string
string = 'javatpoint_is_best_for_coding_tutorials'

# printing initial string
print("The original string is : " + string)

# storing the first and rest using split()
initial, *rest = string.split('_')

# using map() to titeling all the splitted words except the first one
camelcase = ''.join([initial.lower(), *map(str.title, rest)])

# printing result 
print("The camelcase of the  created string is : " + camelcase)

输出:

The original string is : javatpoint_is_best_for_coding_tutorials
The camelcase of the created string is : javatpointIsBestForCodingTutorials

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程