Python基础学习-04

本文介绍了Python中变量类型转换的基本用法,如int(), float(), str(),并详细讲解了字符串处理、字符数组操作、格式化字符串以及常见函数应用。通过实例演示了如何处理整数、浮点数和字符串,以及字符串操作技巧如长度计算、查找、替换和格式设置。
摘要由CSDN通过智能技术生成

Python基础学习-04

# Python specifying variable types

# int() 从整数文字、浮点文字(Delete decimals)或字符串文字(提供字符串表示整数)构造整数
# float() 从整数文字、浮点文字或字符串文字构造浮点数(提供字符串表示浮点数或整数)
# str() 从多种数据类型构造一个字符串,包括字符串、整理文字和浮点文字

# int
x = int(1)  # x will be 1
y = int(2.8)  # y will be 2
z = int("3")  # z will be 3
# float
x2 = float(1)  # x2 will be 1.0
y2 = float(2.8)  # y2 will be 2.8
z2 = float("3")  # z2 will be 3.0
w2 = float("4.2")  # w2 will be 4.2
# str
x1 = str("s1")  # x1 will be 's1'
y1 = str(2)  # y1 will be '2'
z1 = str(3.0)  # z1 will be '3.0'

# Python character string

# The str comprehend
# "hello" == 'hello'
print("hello")
print('hello')
# Distribution of variables
a = "小脑斧"
print(a)
# Multiline string
a = """奇变偶不变
符号看象限
"""
print(a)
a = '''奇变偶不变
符号看象限
'''
print(a)
"""
奇变偶不变
符号看象限 
"""

# string array
a = "hello world!"
print(a[0])
""" h """
# Traversal string
for x in "饿了想吃饭":
    print(x)
""" 
饿
了
想
吃
饭
"""
# string length
a = "hello python!"
print(len(a))
""" 13 """
# Checking string
txt = "this is one words!"
print("one" in txt)  # 返回布尔类型
# 也可以通过if判断
txt = "this is one words!"
if "one" in txt:
    print("是的,one在txt内")
""" 是的,one在txt内 """
# 检查如果不是
txt = "好饿呀,想吃饭了"
print("好饿" not in txt)
""" False """

# Sliced string
# Gets an array of the specified range
b = "hello world"
print(b[2:5])
""" llo """
# Gets the character from the start
b = "hello world"
print(b[:5])
""" hello """
# Gets the character from the end
print(b[2:])
""" llo world """
# The negative index
print(b[-5:-2])
""" wor """


# Modify string
#  Lowercase to uppercase
a = " Hello,World!  "
print(a.upper())
""" HELLO,WORLD! """
#  Uppercase to lowercase
print(a.lower())
""" hello,world!  """
#  Delete the blank space
print(a.strip())
""" Hello,World! """
#  Substitution string
a = "Hello,World!"
print(a.replace("H", "J"))
""" Jello,World! """
#  Split string
print(a.split(","))
""" ['Hello', 'World!'] """
# concatenation of string
a = "hello"
b = "World!"
c = a + b
print(c)
""" helloWorld! """
c = a + " " + b
print(c)
""" hello World! """
# format string
age = 20
txt = "猪猪侠今年{}岁"
print(txt.format(age))
""" 猪猪侠今年20岁 """
# format multiple mass participation
name = "喜羊羊"
age = 15
home = "羊村"
txt = "我叫{},今年{}岁了,住在{}。"
print(txt.format(name,age,home))
""" 我叫喜羊羊,今年15岁了,住在羊村。 """
txt = "我叫{2},今年{0}岁了,住在{1}。"
print(txt.format(age,home,name))
""" 我叫喜羊羊,今年15岁了,住在羊村。 """
# other functions
"""
1. capitalize() 将第一个字符转为大写
2. casefold() 将字符串转换为小写
3. center() 返回一个居中的字符串
4. count() 返回指定值在字符串出现的次数
5. encode() 返回字符串的编码版本
6. endswith() 如果字符串以指定的值结尾,则返回True
7. join() 将可迭代的元素连接的字符串的末尾
8. find() 在字符串中搜索指定值并返回找到它的位置
9. format() 初始化字符串中的指定值
10. index() 在字符串中搜索指定值并返回找到它的位置
"""

# Test
#1->使用len方法打印字符串的长度  ->11
x = "hello world"
print(len(x))
#2->获取字符串txt的第一个字符串  ->H
txt = "Hello world"
x = txt[0]
print(x)
#3->获取索引2到索引4的字符  ->llo
x = txt[2:5]
print(x)
#4->返回开头或结尾没有任何空格的字符串 ->helloWorld!
x = " helloWorld! "
print(x.strip())
#5->将txt值转换为大写 ->HELLO WORLD
txt = "Hello world"
print(txt.upper())
#6->将txt值转换为小写 ->hello world
print(txt.lower())
#7->用J替换字符H ->Jello world
print(txt.replace("H", "J"))
#8->插入正确语法来添加年龄参数的占位符 ->txt = "My name is John, and i am {}"
age = 36
txt = "My name is John, and i am {}"
print(txt.format(age))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七七高7777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值