String Concatenation and Uppercase Conversion Function

  • Share this:

Code introduction


This function takes two string arguments, concatenates them using string formatting, and converts the result to uppercase. If the input arguments are not strings, an exception is raised.


Technology Stack : Exception handling, string formatting

Code Type : Exception Handling and String Formatting

Code Difficulty : Intermediate


                
                    
def aaaa(arg1, arg2):
    """
    使用内置的异常处理和字符串格式化功能来转换字符串列表为大写。
    """
    try:
        # 确保arg1和arg2都是字符串类型
        if not isinstance(arg1, str) or not isinstance(arg2, str):
            raise ValueError("Both arguments must be strings.")
        
        # 使用字符串格式化来合并两个字符串
        result = f"{arg1} {arg2}"
        # 返回大写的结果
        return result.upper()
    except Exception as e:
        # 返回异常信息
        return str(e)