在Python中,表示序数(如1st、2nd、3rd等)通常使用字符串格式化。以下是一个示例代码,展示了如何使用f-string(Python 3.6及以上版本)来格式化序数:
num = 1 示例数字ordinal_suffix = {1: "st",2: "nd",3: "rd",4: "th",5: "th",6: "th",7: "th",8: "th",9: "th",10: "th",11: "th",12: "th",13: "th",14: "th",15: "th",16: "th",17: "th",18: "th",19: "th",20: "th",21: "st",22: "nd",23: "rd",24: "th",25: "th",26: "th",27: "th",28: "th",29: "th",30: "th",31: "st",32: "nd",33: "rd",34: "th",35: "th",36: "th",37: "th",38: "th",39: "th",40: "th",41: "st",42: "nd",43: "rd",44: "th",45: "th",46: "th",47: "th",48: "th",49: "th",50: "th",51: "st",52: "nd",53: "rd",54: "th",55: "th",56: "th",57: "th",58: "th",59: "th",60: "th",61: "st",62: "nd",63: "rd",64: "th",65: "th",66: "th",67: "th",68: "th",69: "th",70: "th",71: "st",72: "nd",73: "rd",74: "th",75: "th",76: "th",77: "th",78: "th",79: "th",80: "th",81: "st",82: "nd",83: "rd",84: "th",85: "th",86: "th",87: "th",88: "th",89: "th",90: "th",91: "st",92: "nd",93: "rd",94: "th",95: "th",96: "th",97: "th",98: "th",99: "th",100: "th",}print(f"{num}{ordinal_suffix[num]}") 使用f-string格式化序数

这段代码定义了一个字典`ordinal_suffix`,其中包含了从1到100的数字对应的序数后缀。然后使用f-string格式化输出序数,例如`1st`、`2nd`等。
如果你使用的是Python 3.5或更早的版本,可以使用`str.format`方法或者百分号格式化(%)来实现相同的效果。
希望这能帮助你理解如何在Python中表示序数
