python字符串内建函数-ljust、rjust、center

python字符串内建函数-ljust、rjust、center

11、ljust

函数功能:该函数用于将字符串进行左对齐,并使用空格填充至指定长度的新字符串

返回值:该函数的返回值左对齐后的新字符串

函数语法str.ljust(width,[,fillchar])

  • width:指定字符串的长度
  • fillchar:填充字符,默认为空格。
# ljust
str1 = "hello world I love python"
print(str1.ljust(30,"*")) # hello world I love python*****

12、rjust

函数功能:该函数用于将字符串进行右对齐,并使用空格填充至指定长度的新字符串

返回值:该函数的返回值右对齐后的新字符串

函数语法str.rjust(width,[,fillchar])

  • width:指定字符串的长度
  • fillchar:填充字符,默认为空格。
# rjust
str1 = "hello world I love python"
print(str1.rjust(30,"*")) # *****hello world I love python

13、center

函数功能:该函数用于将字符串进行居中对齐,并使用空格填充至指定长度的新字符串

返回值:该函数的返回值居中对齐后的新字符串

函数语法str.center(width,[,fillchar])

  • width:指定字符串的长度
  • fillchar:填充字符,默认为空格。
# center
str1 = "hello world I love python"
print(str1.center(30,"*")) # **hello world I love python***

注:python字符串内建函数还有很多,查看更多点击下面链接:

  • python 字符串内建函数

  • python 字符串内建函数-find、index、count

  • python字符串内建函数-replace、split

  • python字符串内建函数-capitalize、title、upper

  • python字符串内建函数-startwith、endwith

  • python字符串内建函数-lstrip 、rstrip、strip

你可能感兴趣的:(python,#,基础知识,python,字符串)